Open In App

Spring – Autowiring

Last Updated : 04 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Autowiring in 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. To enable Autowiring in the Spring application we should use @Autowired annotation. Autowiring in Spring internally uses constructor injection. 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

Modes

Description

No

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

byName

It uses the name of the bean for injecting dependencies.

byType

It injects the dependency according to the type of bean.

Constructor

It injects the required dependencies by invoking the constructor.

Autodetect

The autodetect mode uses two other modes for autowiring – constructor and byType.

1. No

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

<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.

<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.

<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.

<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.

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

Example of Autowiring

Sample Program for the byType Mode:

State.java

Java




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


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

Note:

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 and It can’t be used for primitive and string values.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads