๐ŸŽ‰ 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 : byType"

  • Definition : In this mode, Spring will inject the dependency based on the type of the property. If a bean of the matching type is found in the container, it will be injected.
  • Example :
    <bean id="engine" class="com.example.Engine" />
    <bean id="car" class="com.example.Car" autowire="byType" />
  • Explanation : Here, Spring will match the property type of the Car class (e.g., Engine type) and inject the corresponding bean (engine).
  • Internal Behavior : By default, byType relies on setter injection to match and inject dependencies based on their type. In cases where the class defines a suitable constructor, Spring can automatically switch to constructor injection.
  • 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;
    
        // Setter for dependency injection
        public void setEngine(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 autowiring byName -->
        <bean id="car" class="in.sp.beans.Car" autowire="byType" />
    
    </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 byType Autowiring Works ?
    1. Configuration Explanation:
      • The Car bean is configured with autowire="byType".
      • Spring looks for a bean whose type matches the property type in the Car class. In this case, Spring will look for a bean of type Engine to inject into the Car class.
      • The engine bean is explicitly defined with the class <bean id="engine" class="in.sp.beans.Engine" />.
      • Since the Car class has a property of type Engine and the engine bean is of type Engine, Spring automatically injects the engine bean into the Car bean.
    2. Car Class:
      • The Car class has a property private Engine engine;, so Spring will search for a bean of type Engine to inject into this property using setter method i.e. setEngine(Engine engine).
    3. Engine Bean:
      • The Engine bean is explicitly defined in the XML configuration i.e.
        <bean id="engine" class="in.sp.beans.Engine" />
      • The Engine bean is of the correct type (Engine), so it is automatically injected into the Car bean's engine property by Spring, due to the autowire="byType" setting.