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

  • Definition : In this mode, Spring will attempt to match the property name with a bean id in the container. If a match is found, it will inject the dependency.
  • Example :
    <bean id="engine" class="com.example.Engine" />
    <bean id="car" class="com.example.Car" autowire="byName" />
  • Explanation : Here, Spring will inject the engine bean into the car bean because the name engine matches the property name in the Car class.
  • Internal Behavior : By default, byName uses setter injection to inject dependencies. However, if the Java class has an explicit constructor that matches the bean, Spring may opt for constructor injection instead.
  • 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="byName" />
    
    </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 byName Autowiring Works ?
    1. Configuration Explanation:
      • The Car bean is configured with autowire="byName".
      • Spring looks for a bean with an ID matching the property name (engine) in the Car class.
      • The engine bean is found (defined as <bean id="engine" ... />) and automatically injected into the Car bean.
    2. Car Class:
      • The Car class has a property private Engine engine; with a setter method setEngine().
    3. Engine Bean:
      • The engine bean is explicitly defined with the matching ID engine.