• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Spring Autowiring using Annotation – @Autowired, @Resource, @Qualifier, @Inject Annotation

June 14, 2016 by javainterviewpoint Leave a Comment

Spring Autowiring by using the “autowire” attribute in the bean configuration file  we can wire all the properties of the bean class. Using Spring Autowiring through XML you cannot wire a particular property. In those cases we can use the Spring @Autowired annotation which allows auto-wiring of setter method, a constructor, a field, or even an arbitrary method.

Folder Structure:

  1. Create a new Java Project  “SpringCoreTutorial” and create a package for our src files “com.javainterviewpoint“
  2. Add the required libraries to the build path. Java Build Path ->Libraries ->Add External JARs and add the below jars.

    commons-logging-1.2.jar
    spring-beans-4.2.4.RELEASE.jar
    spring-core-4.2.4.RELEASE.jar
    spring-context-4.2.4.RELEASE.jar
    spring-expression-4.2.4.RELEASE.jar

  3. Create the Java classes Employee.java, PermanentEmployee.java and EmployeeLogic.java under  com.javainterviewpoint folder.
  4. Place our configuration file SpringConfig.xml in the src directory

SpringConfig.xml

<beans xmlns="http://www.springframework.org/schema/beans"
 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
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
   <context:annotation-config></context:annotation-config>
 
   <bean id="emp" class="com.javainterviewpoint.Employee"></bean>
 
   <bean id="permemp" class="com.javainterviewpoint.PermanentEmployee"></bean>
</beans>
  • We have declared two beans one for our Employee class and and other for our PermanentEmployee class, we have not injected any reference to the property “pe” of our Employee class.

Spring AutoWiring @Autowired Annotation over Setter Method

@Autowired annotation can be applied to any particular property, in this Spring autowiring example lets autowire the setter method of the “pe” property with @Autowired annotation. Spring container will try to wire a bean which is compatible to type “PermanentEmployee”

Employee.java

Our Employee class has a property “pe” and we have added the @Autowire annotation over its setter method.

package com.javainterviewpoint;

import org.springframework.beans.factory.annotation.Autowired;

public class Employee
{
    
    private PermanentEmployee pe;

    public Employee()
    {
        super();
    }
    public Employee(PermanentEmployee pe)
    {
        super();
        this.pe = pe;
    }
    
    public PermanentEmployee getPe()
    {
        return pe;
    }
    @Autowired
    public void setPe(PermanentEmployee pe)
    {
        this.pe = pe;
    }
}

PermanentEmployee.java

package com.javainterviewpoint;

public class PermanentEmployee
{
    private Employee employee;
    private int Salary;
    
    public PermanentEmployee()
    {
        super();
    }
    public PermanentEmployee(Employee employee, int salary)
    {
        super();
        this.employee = employee;
        Salary = salary;
    }
    
    public Employee getEmployee()
    {
        return employee;
    }
    public void setEmployee(Employee employee)
    {
        this.employee = employee;
    }
    public int getSalary()
    {
        return Salary;
    }
    public void setSalary(int salary)
    {
        Salary = salary;
    }
}

EmployeeLogic.java

package com.javainterviewpoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EmployeeLogic
{
    public static void main(String args[])
    {
        //Read the Configuration file using ApplicationContext
        ApplicationContext applicationContext = 
                new ClassPathXmlApplicationContext("SpringConfig.xml");
        //Get the Employee class instance
        Employee employee = (Employee)applicationContext.getBean("emp");
        //Print the PermanentEmployee details
        System.out.println("**** Employee Details ****");
        //Setting the salary 
        employee.getPe().setSalary(100);
        //Retrieving the Permanent Employee salary
        System.out.println(employee.getPe().getSalary());
    }
}
  • In our EmployeeLogic class we have read the Configuration file(SpringConfig.xml) and get all the bean definition through ApplicationContext
  • Get the Employee Class instance by calling the getBean() method over the context created.
  • Through the getPe() we will get the “PermanentEmployee” and using it we will be setting and retrieving the value of the salary property.

Output :

Upon running the EmployeeLogic class we will be getting the below output.

Spring Autowired Annotation Example

Spring @Autowired Annotation over Constructor

In addition to Setter method @Autowired annotation can be applied to the Constructor as well, spring will try to wire the bean compatible type of each Constructor argument. Here PermanentEmployee is the constructor argument, so the bean compatible to type PermanentEmployee will be injected.

package com.javainterviewpoint;

import org.springframework.beans.factory.annotation.Autowired;

public class Employee
{
    
    private PermanentEmployee pe;

    public Employee()
    {
        super();
    }
    @Autowired
    public Employee(PermanentEmployee pe)
    {
        super();
        this.pe = pe;
    }
    
    public PermanentEmployee getPe()
    {
        return pe;
    }
    public void setPe(PermanentEmployee pe)
    {
        this.pe = pe;
    }
}

Spring @Autowired Annotation over array / collections

@Autowired annotation can also be applied to a property of array type or java collection. Lets say, if you annotate the property PermanentEmployee[] or List<PermanentEmployee> with @Autowired, Spring will auto-wire all the beans whose type is compatible with PermanentEmployee.

package com.javainterviewpoint;

import org.springframework.beans.factory.annotation.Autowired;

public class Employee
{
    @Autowired
    private PermanentEmployee[] pe;

    public Employee()
    {
        super();
    }
    public Employee(PermanentEmployee pe)
    {
        super();
        this.pe = pe;
    }
    
    public PermanentEmployee getPe()
    {
        return pe;
    }
    public void setPe(PermanentEmployee pe)
    {
        this.pe = pe;
    }
}

required attribute @Autowired annotation

By default, all the properties with @Autowired are required. Whenever Spring can’t find a matching bean to wire, it will be throwing BeanCreationException.

BeanCreationException

But there will time when you want some property to be optional, then we can set the “required” attribute of @Autowired to false so if Spring cannot find the matching bean it will not be throwing exception.

package com.javainterviewpoint;

import org.springframework.beans.factory.annotation.Autowired;

public class Employee
{
    
    private PermanentEmployee pe;

    public Employee()
    {
        super();
    }
    public Employee(PermanentEmployee pe)
    {
        super();
        this.pe = pe;
    }
    
    public PermanentEmployee getPe()
    {
        return pe;
    }
    @Autowired(required = false)
    public void setPe(PermanentEmployee pe)
    {
        this.pe = pe;
    }
}

Use of @Qualifier annotation Spring – AutoWiring byName

Autowiring byType will not work when there is more than one bean of same type declared. We will be getting BeanCreationException / NoUniqueBeanDefinitionException Exception

 Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.javainterviewpoint.Employee.setPe(com.javainterviewpoint.PermanentEmployee); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.javainterviewpoint.PermanentEmployee] is defined: expected single matching bean but found 2: permemp1,permemp2
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:661)
 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
 ... 13 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.javainterviewpoint.PermanentEmployee] is defined: expected single matching bean but found 2: permemp1,permemp2
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:618)
 ... 15 more

Spring AutoWiring has the solution for it, we can use the @Qualifier annotation by providing the name of the required bean.

Our configuration file will be like below

<beans xmlns="http://www.springframework.org/schema/beans"
 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
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
   <context:annotation-config></context:annotation-config>
 
   <bean id="emp" class="com.javainterviewpoint.Employee"></bean>
 
   <bean id="permemp" class="com.javainterviewpoint.PermanentEmployee"></bean>
   <bean id="permemp2" class="com.javainterviewpoint.PermanentEmployee"></bean>
</beans>

Now we can use the @Qualifier annotation to select the required property type. Now our Employee class will be re-written like below

package com.javainterviewpoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Employee
{
    
    private PermanentEmployee pe;

    public Employee()
    {
        super();
    }
   
    public Employee(PermanentEmployee pe)
    {
        super();
        this.pe = pe;
    }
    
    public PermanentEmployee getPe()
    {
        return pe;
    }
    @Autowired
    @Qualifier("permemp2")
    public void setPe(PermanentEmployee pe)
    {
        this.pe = pe;
    }
}

Spring @Resource annotation

When ever you want to implement Spring autowiring byName using annotation, you can annotate a setter method, a constructor, or a field with @Resource annotation which is based on JSR-250. Spring will attempt to find if any bean is declared in the configuration file with property name. You can also specify the bean name explicitly using the name attribute.

<beans xmlns="http://www.springframework.org/schema/beans"
 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
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
   <context:annotation-config></context:annotation-config>
 
   <bean id="emp" class="com.javainterviewpoint.Employee"></bean>
 
  <bean id="pe" class="com.javainterviewpoint.PermanentEmployee"></bean>
   <bean id="permemp2" class="com.javainterviewpoint.PermanentEmployee"></bean>
</beans>

Now we can use the @Qualifier annotation to select the required property type. Now our Employee class will be re-written like below

package com.javainterviewpoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Employee
{
    
    private PermanentEmployee pe;

    public Employee()
    {
        super();
    }
   
    public Employee(PermanentEmployee pe)
    {
        super();
        this.pe = pe;
    }
    
    public PermanentEmployee getPe()
    {
        return pe;
    }
    @Resource(name="pe")
    public void setPe(PermanentEmployee pe)
    {
        this.pe = pe;
    }
}

@Inject annotation Spring

Spring 3.0 supports JSR 330 standard : Dependency Injection for Java. In Spring 3 application, we can use @Inject instead of Spring’s @Autowired to inject a bean. The JSR 330 Standard @Inject annotation works exactly the same like Spring’s @Autowired annotation. In order to use @Inject annotation we need to add “javax.inject-1.jar” into our project.

package com.javainterviewpoint;

import javax.inject.Inject;

public class Employee
{
    
    private PermanentEmployee pe;

    public Employee()
    {
        super();
    }
   
    public Employee(PermanentEmployee pe)
    {
        super();
        this.pe = pe;
    }
    
    public PermanentEmployee getPe()
    {
        return pe;
    }
   @Inject
    public void setPe(PermanentEmployee pe)
    {
        this.pe = pe;
    }
}

Pitfalls of @Inject over @Autowired

  1. @Inject annotation doesn’t have the required attribute unlike @Autowired annotation, so that we can make a filed mandatory or optional.
  2. JSR 330 @Inject annotation is Singleton by default. But in Spring we can use other scopes as well using the @Scopes annotation.

Difference Between @Resource, @Autowired and @Inject in Spring Injection

Other interesting articles which you may like …

  • Spring 3 Hello World Example
  • Spring 3 JavaConfig Example
  • Spring 3 @Import annotation with JavaConfig Example
  • Dependency Injection In Spring
  • Spring Dependency Injection – Setter Injection
  • Spring Setter Injection With Primitive data types
  • Spring Setter Injection With Objects
  • Spring Dependency Injection With List Collection Example
  • Spring Dependency Injection With Set Collection Example
  • Spring Dependency Injection With Map Collection Example
  • Spring Dependency Injection With Properties Example
  • Spring Dependency Injection – Constructor Injection
  • Spring Difference between Setter and Constructor Injection

Filed Under: J2EE, Java, Spring, Spring Core, Spring Tutorial Tagged With: @Autowired, @Inject, @Qualifier, @Resource, Annotation, Spring Autowiring

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·