The Spring framework provides an autowiring feature to enable the object dependency implicitly. @Autowired and @Qualifier are the two different autowire annotations to achieve the automatic dependency injunction in Spring.

Spring @Autowired annotation :

In Spring, we can use @Autowired annotation to tell the IOC container that the dependency of the properties will be injected by the container itself.

The @Autowired annotations can be used on top of a field, setter methods or on top of a constructor of a class.

If we declare the autowire annotation on top of the method or property, the IOC container searches whether a dependency property name and the bean id in the XML are matched or not, if those are matched the automatic dependency injection will happen.

@Autowired with Setter :

Book.java
public class Book {
    private Author author;

    @Autowired
    public void setAuthor(Author author) {
        this.author = author;
    }
}

Xml Configuration :

spring.xml
<bean id="author" class="com.onlinetutorialspoint.beans.Author">   
   <property name="name" value="OnlineTutorialsPoint" />       
   <property name="name" value="Hyderabad" />
</bean>

@Autowired with Constructor :

We can use the @Autowired annotation, on top of the constructor.

Here is the syntax:

Book.java
public class Book {
    private Author author;

    @Autowired
    public Book(Author author) {
        this.author = author;
    }
}

@Autowired with Property :

We can use the Autowired annotation, on top of the property. Here is the syntax:

Book.java
public class Book {
    @Autowired
    private Author author;
}

Xml Configuration :

spring.xml
<bean id="author" class="com.onlinetutorialspoint.beans.Author">
   <property name="name" value="OnlineTutorialsPoint" />     
   <property name="name" value="Hyderabad" />
</bean>

Spring @Qualifier annotation :

@Qualifier is one of the autowiring properties in spring. If a dependency class is configured more than once in the spring configuration file, the IOC container will throw the Exception for bean ambiguity. In order to resolve the ambiguity, we can use @Qualifier annotation along with the  @Autowired annotation.

Example:

Book.java
public class Book {
    private Author author;
    @Autowired
    @Qualifier("author2")
    private Author author;

    public void setAuthor(Author author){
        this.author = author
    }
}

Xml Configuration :

spring.xml
<bean id="author" class="com.onlinetutorialspoint.beans.Author">
   <property name="name" value="OnlineTutorialsPoint" />
   <property name="name" value="Hyderabad" />
</bean>

Spring @Value annotation :

@Value annotation is used to assign a value to a property. It can be used to assign a value for simple type properties like int, float etc.. the @Value annotation tells IOC container, that read the value of a key from the resource bundle and initializes a simple property with that value.

Example:

Sample.java
public class Sample {
    @Value("${k1}")
    private int i;
}

At runtime, the IOC container initializes the i with the value of k1 from the resource bundle. Here the resource bundle can be a properties or yaml file. In the properties file, if the k1 value is 100 then in runtime IOC container assign the 100 value to i

References:

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment