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

  • Definition : In this mode, no autowiring is performed. Dependencies must be explicitly defined using <property> or <constructor-arg> tags.
  • Example :
    <bean id="engine" class="com.example.Engine" />
    <bean id="car" class="com.example.Car">
        <property name="engine" ref="engine" />
    </bean>
  • Explanation : Here, no autowiring is used. The dependency (engine) is explicitly provided using the <property> tag. Spring will not attempt to inject dependencies automatically.
  • Internal Behavior : In this mode, no automatic injection is performed. All dependencies must be explicitly defined using <property> or <constructor-arg> tags in the XML configuration. Spring does not attempt to inject anything by itself.
  • Now we will create 2 programs of Autowiring using XML Based Configuration
    1. Using <constructor-arg> tag.
    2. Using <property> tag.

  • Program 1
    First we will create program demonstrating autowiring mode as no using <constructor-arg> tag.
    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-based dependency injection -->
        <bean id="car" class="in.sp.beans.Car">
            <constructor-arg ref="engine" />
        </bean>
    
    </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...


  • Program 2
    Now we will create second program demonstrating autowiring mode as no using <property> tag.
    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 method 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 explicit dependency injection -->
        <bean id="car" class="in.sp.beans.Car">
            <property name="engine" ref="engine" />
        </bean>
    
    </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...