Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Spring – Autowiring

  • Last Updated : 19 Nov, 2021

Spring is an open-source application development framework of Java that allows you to create robust enterprise applications using Plain Old Java Objects (POJO in short). The Spring framework can inject dependencies automatically. The Spring container detects those dependencies specified in the configuration file and @ the relationship between the beans. This is referred to as autowiring in Spring. An autowired application requires fewer lines of code comparatively but at the same time, it provides very little flexibility to the programmer.

Modes of Autowiring

There are five modes of autowiring:

1. No

This mode tells the framework that autowiring is not supposed to be done. It is the default mode used by Spring.

XML






<bean id="state" class="sample.State">
 <property name="name" value="UP" />
</bean>
<bean id="city" class="sample.City"></bean>


2. byName

It uses the name of the bean for injecting dependencies. However, it requires that the name of the property and bean must be the same. It invokes the setter method internally for autowiring.

XML




<bean id="state" class="sample.State">
 <property name="name" value="UP" />
</bean>
<bean id="city" class="sample.City" autowire="byName"></bean>


3. byType

It injects the dependency according to the type of the bean. It looks up in the configuration file for the class type of the property. If it finds a bean that matches, it injects the property. If not, the program throws an error. The names of the property and bean can be different in this case. It invokes the setter method internally for autowiring.

XML




<bean id="state" class="sample.State">
 <property name="name" value="UP" />
</bean>
<bean id="city" class="sample.City" autowire="byType"></bean>


4. constructor

It injects the required dependencies by invoking the constructor. It works similar to the “byType” mode but it looks for the class type of the constructor arguments. If none or more than one bean are detected, then it throws an error, otherwise, it autowires the “byType” on all constructor arguments.



XML




<bean id="state" class="sample.State">
 <property name="name" value="UP" />
</bean>
<bean id="city" class="sample.City" autowire="constructor"></bean>


5. autodetect

The autodetect mode uses two other modes for autowiring – constructor and byType. It first tries to autowire via the constructor mode and if it fails, it uses the byType mode for autowiring. It works in Spring 2.0 and 2.5 but is deprecated from Spring 3.0 onwards.

XML




<bean id="state" class="sample.State">
 <property name="name" value="UP" />
</bean>
<bean id="city" class="sample.City" autowire="autodetect"></bean>


Sample Program for the byType Mode

File Name: State.java

Java




public class State {
    private String name;
    public String getName() { return name; }
    public void setName(String s) { this.name = s; }
}


File Name: City.java

Java




class City {
    private int id;
    private String name;
    private State s;
    public int getID() { return id; }
    public void setId(int eid) { this.id = eid; }
    public String getName() { return name; }
    public void setName(String st) { this.name = st; }
    public State getState() { return s; }
    @Autowired public void setState(State sta)
    {
        this.s = sta;
    }
    public void showCityDetails()
    {
        System.out.println("City Id : " + id);
        System.out.println("City Name : " + name);
        System.out.println("State : " + s.getName());
    }
}


Spring bean configuration file:

XML




<?xml version="1.0" encoding="UTF-8"?>
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
<bean id="state" class="sample.State">
<property name="name" value="UP" />
</bean>
<bean id="city" class="sample.City" autowire="byName"></bean>
</beans>


Test Program file: DemoApplication.java

Java




@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args)
    {
        SpringApplication.run(DemoApplication.class, args);
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        City cty = context.getBean("city", City.class);
        cty.setId(01);
        cty.setName("Varanasi");
        State st = context.getBean("state", State.class);
        st.setName("UP");
        cty.setState(st);
        cty.showCityDetails();
    }
}


Output: 

City ID : 01
City Name : Varanasi
State : UP

Advantage of Autowiring

It requires less code because we don’t need to write the code to inject the dependency explicitly.

Disadvantage of Autowiring

  • No control of the programmer.
  • It can’t be used for primitive and string values.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!