๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Autowiring in Spring using XML Configuration


Autowiring using XML Configuration with "Autowiring Mode : constructor"

  • Definition : In this mode, Spring will inject the dependency through the constructor. If there are multiple constructors, Spring will try to find the one that matches the types of the parameters.
  • Example :
    <bean id="engine" class="com.example.Engine" />
    <bean id="car" class="com.example.Car" autowire="constructor" />
  • Explanation : Here, Spring will inject the engine bean into the Car bean's constructor based on the constructor parameter type.
  • Internal Behavior : In this mode, Spring always uses constructor injection. It resolves dependencies by matching the bean type to the constructor parameters. If multiple constructors exist, Spring tries to match the most specific one with the available beans.
  • Program
    Engine.java
    package in.sp.beans;
    
    public class Engine
    {
        public void start()
        {
            System.out.println("Engine started...");
        }
    }
    Car.java
    package in.sp.beans;
    
    public class Car
    {
        private Engine engine;
    
        // Constructor for dependency injection
        public Car(Engine engine)
        {
            this.engine = engine;
        }
    
        public void drive() 
        {
            engine.start();
            System.out.println("Car is running...");
        }
    }
    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!-- Define Engine bean -->
        <bean id="engine" class="in.sp.beans.Engine" />
    
        <!-- Define Car bean with constructor autowiring -->
        <bean id="car" class="in.sp.beans.Car" autowire="constructor" />
    
    </beans>
    
    MainApp.java
    package in.sp.main;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import in.sp.beans.Car;
    
    public class MainApp
    {
    	public static void main(String[] args)
    	{
    		// Load Spring context from XML configuration
            ApplicationContext context = new ClassPathXmlApplicationContext("in/sp/resources/applicationContext.xml");
    
            // Retrieve the Car bean
            Car car = context.getBean(Car.class);
    
            // Call the drive method
            car.drive();
    	}
    }
    Below is the output
    Output:
    Engine started...
    Car is running...


  • How constructor Autowiring Works ?
    1. Configuration Explanation:
      • The Car bean is configured with autowire="constructor".
      • Spring will automatically look for a constructor in the Car class that takes an argument of type Engine and inject the engine bean into it.
    2. Constructor Injection:
      • In the Car class, the constructor public Car(Engine engine) is used to inject the engine dependency. Spring automatically matches the constructor argument type with the type of bean (Engine).
    3. No Explicit <property> or <constructor-arg> Tags:
      • Since autowire="constructor" is used, Spring automatically provides the Engine bean to the Car bean through the constructor, and no need for explicit <constructor-arg> tags.