Spring @Autowired Annotation

Filed Under: Spring

Spring @Autowired annotation is used for automatic dependency injection. Spring framework is built on dependency injection and we inject the class dependencies through spring bean configuration file.

Spring @Autowired Annotation

spring @autowired annotation, @Autowired, spring autowiring, spring @autowired

Usually we provide bean configuration details in the spring bean configuration file and we also specify the beans that will be injected in other beans using ref attribute. But Spring framework provides autowiring features too where we don’t need to provide bean injection details explicitly.

There are different ways through which we can autowire a spring bean.

  1. autowire byName – For this type of autowiring, setter method is used for dependency injection. Also the variable name should be same in the class where we will inject the dependency and in the spring bean configuration file.
  2. autowire byType – For this type of autowiring, class type is used. So there should be only one bean configured for this type in the spring bean configuration file.
  3. autowire by constructor – This is almost similar to autowire byType, the only difference is that constructor is used to inject the dependency.
  4. autowire by autodetect – If you are on Spring 3.0 or older versions, this is one of the autowire options available. This option was used for autowire by constructor or byType, as determined by Spring container. Since we already have so many options, this option is deprecated. I will not cover this option in this tutorial.
  5. @Autowired annotation – We can use Spring @Autowired annotation for spring bean autowiring. @Autowired annotation can be applied on variables and methods for autowiring byType. We can also use @Autowired annotation on constructor for constructor based spring autowiring.

    For @Autowired annotation to work, we also need to enable annotation based configuration in spring bean configuration file. This can be done by context:annotation-config element or by defining a bean of type org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.

  6. @Qualifier annotation – This annotation is used to avoid conflicts in bean mapping and we need to provide the bean name that will be used for autowiring. This way we can avoid issues where multiple beans are defined for same type. This annotation usually works with the @Autowired annotation. For constructors with multiple arguments, we can use this annotation with the argument names in the method.

By default spring bean autowiring is turned off. Spring bean autowire default value is “default” that means no autowiring is to be performed. autowire value “no” also have the same behavior.

To showcase the use of Spring Bean autowiring, let’s create a simple Spring Maven project. Our final project will look like below image.

Spring @Autowired, Spring autowiring, @Autowired annotation

Let’s look into each of the autowire options one by one. For that we will create a Model bean and a service class where we will inject the model bean.

Spring @Autowired Annotation – Maven Dependencies

For spring autowiring, we don’t need to add any additional dependencies. Our pom.xml file has spring framework core dependencies and looks like below.

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">


		<!-- Generic properties -->

		<!-- Spring -->

		<!-- Logging -->


		<!-- Spring and Transactions -->

		<!-- Logging with SLF4J & LogBack -->


Spring @Autowired Annotation – Model Bean

Let’s create a simple Java Bean, named Employee. This bean will have a single property with getter and setter methods. We will initialize this property value in the spring bean configuration file.

package com.journaldev.spring.autowiring.model;

public class Employee {

	private String name;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

Spring @Autowired Annotation – Service Class

Let’s create our service class in which we will inject Employee bean through spring autowiring.

package com.journaldev.spring.autowiring.service;

import com.journaldev.spring.autowiring.model.Employee;

public class EmployeeService {

	private Employee employee;

	// constructor is used for autowire by constructor
	public EmployeeService(Employee emp) {
		System.out.println("Autowiring by constructor used");
		this.employee = emp;

	// default constructor to avoid BeanInstantiationException for autowire
	// byName or byType
	public EmployeeService() {
		System.out.println("Default Constructor used");

	// used for autowire byName and byType
	public void setEmployee(Employee emp) {
		this.employee = emp;

	public Employee getEmployee() {
		return this.employee;

We will use the same service class for perform spring autowiring byName, byType and by constructor. The setter method will be used for spring autowiring byName and byType whereas constructor based injection will be used by constructor autowire attribute.

When we use spring autowire byName or byType, default constructor is used. That’s why we have explicitly defined the default constructor for the EmployeeService bean.

Spring @Autowired Annotation – autowiring byType Example

Let’s create a separate class with Spring @Autowired annotation for autowiring byType.

package com.journaldev.spring.autowiring.service;

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

import com.journaldev.spring.autowiring.model.Employee;

public class EmployeeAutowiredByTypeService {

	//Autowired annotation on variable/setters is equivalent to autowire="byType"
	private Employee employee;
	public void setEmployee(Employee emp){
	public Employee getEmployee(){
		return this.employee;

Note that I have annotated both Employee variable and it’s setter method with Spring @Autowired annotation, however only one of these is sufficient for spring bean autowiring.

Spring @Autowired Annotation and @Qualifier Bean autowiring by constructor Example

Let’s create another service class where we will use @Autowired annotation for constructor based injection. We will also see @Qualifier annotation usage.

package com.journaldev.spring.autowiring.service;

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

import com.journaldev.spring.autowiring.model.Employee;

public class EmployeeAutowiredByConstructorService {

	private Employee employee;

	//Autowired annotation on Constructor is equivalent to autowire="constructor"
	public EmployeeAutowiredByConstructorService(@Qualifier("employee") Employee emp){
	public Employee getEmployee() {
		return this.employee;

When this bean will be initialized by Spring framework, bean with name as “employee” will be used for autowiring. Spring @Autowired annotation excepts one argument “required” that is a boolean with default value as TRUE. We can define it to be “false” so that spring framework don’t throw any exception if no suitable bean is found for autowiring.

Spring @Autowired Annotation – Bean Configuration File

Spring bean configuration file is the main part of any spring application, let’s see how our spring bean configuration file looks and then we will look into each part of it.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
	xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-4.0.xsd"
		default-autowire="byName" default-autowire-candidates="*" >

<bean name="employee" class="com.journaldev.spring.autowiring.model.Employee">
	<property name="name" value="Pankaj"></property>

<bean name="employee1" class="com.journaldev.spring.autowiring.model.Employee" autowire-candidate="false">
	<property name="name" value="Dummy Name"></property>

<!-- autowiring byName, bean name should be same as the property name -->
<bean name="employeeServiceByName" class="com.journaldev.spring.autowiring.service.EmployeeService" autowire="byName" />

<!-- autowiring byType, there should be only one bean definition for the mapping -->
<bean name="employeeServiceByType" class="com.journaldev.spring.autowiring.service.EmployeeService" autowire="byType" />

<!-- autowiring by constructor -->
<bean name="employeeServiceConstructor" class="com.journaldev.spring.autowiring.service.EmployeeService" autowire="constructor" />

<!-- Enable Annotation based configuration -->
<context:annotation-config />

<!-- using @Autowiring annotation in below beans, byType and constructor -->
<bean name="employeeAutowiredByTypeService" class="com.journaldev.spring.autowiring.service.EmployeeAutowiredByTypeService" />
<bean name="employeeAutowiredByConstructorService" class="com.journaldev.spring.autowiring.service.EmployeeAutowiredByConstructorService" />

Important points about spring bean configuration file are:

  • beans element default-autowire is used to define the default autowiring method. Here I am defining the default autowiring method to be byName.
  • beans element default-autowire-candidates is used to provide the pattern for bean names that can be used for autowiring. For simplicity I am allowing all the bean definitions to be eligible for autowiring, however if we can define some pattern for autowiring. For example, if we want only DAO bean definitions for autowiring, we can specify it as default-autowire-candidates="*DAO".
  • autowire-candidate="false" is used in a bean definition to make it ineligible for autowiring. It’s useful when we have multiple bean definitions for a single type and we want some of them not to be autowired. For example, in above spring bean configurations “employee1” bean will not be used for autowiring.
  • autowire attribute byName, byType and constructor is self understood, nothing much to explain there.
  • context:annotation-config is used to enable annotation based configuration support. Notice that employeeAutowiredByTypeService and employeeAutowiredByConstructorService beans don’t have autowire attributes.

Spring @Autowired Annotation – Test Program

Now that our spring application is ready with all types of spring autowiring, let’s write a simple test program to see if it works as expected or not.

package com.journaldev.spring.autowiring.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.journaldev.spring.autowiring.service.EmployeeAutowiredByConstructorService;
import com.journaldev.spring.autowiring.service.EmployeeAutowiredByTypeService;
import com.journaldev.spring.autowiring.service.EmployeeService;

public class SpringMain {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
		EmployeeService serviceByName = ctx.getBean("employeeServiceByName", EmployeeService.class);
		System.out.println("Autowiring byName. Employee Name="+serviceByName.getEmployee().getName());
		EmployeeService serviceByType = ctx.getBean("employeeServiceByType", EmployeeService.class);
		System.out.println("Autowiring byType. Employee Name="+serviceByType.getEmployee().getName());
		EmployeeService serviceByConstructor = ctx.getBean("employeeServiceConstructor", EmployeeService.class);
		System.out.println("Autowiring by Constructor. Employee Name="+serviceByConstructor.getEmployee().getName());
		//printing hashcode to confirm all the objects are of different type
		//Testing @Autowired annotations
		EmployeeAutowiredByTypeService autowiredByTypeService = ctx.getBean("employeeAutowiredByTypeService",EmployeeAutowiredByTypeService.class);
		System.out.println("@Autowired byType. Employee Name="+autowiredByTypeService.getEmployee().getName());

		EmployeeAutowiredByConstructorService autowiredByConstructorService = ctx.getBean("employeeAutowiredByConstructorService",EmployeeAutowiredByConstructorService.class);
		System.out.println("@Autowired by Constructor. Employee Name="+autowiredByConstructorService.getEmployee().getName());


The program is simple, we are just creating the spring application context and using it to get different beans and printing the employee name.

When we run above application, we get following output.

Mar 31, 2014 10:41:58 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3fa99295: startup date [Mon Mar 31 22:41:58 PDT 2014]; root of context hierarchy
Mar 31, 2014 10:41:58 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Default Constructor used
Default Constructor used
Autowiring by constructor used
Autowiring byName. Employee Name=Pankaj
Autowiring byType. Employee Name=Pankaj
Autowiring by Constructor. Employee Name=Pankaj
@Autowired byType. Employee Name=Pankaj
@Autowired by Constructor. Employee Name=Pankaj
Mar 31, 2014 10:41:58 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@3fa99295: startup date [Mon Mar 31 22:41:58 PDT 2014]; root of context hierarchy

As you can see that for autowire byName and byType, default no-args constructor is used to initialize the bean. For autowire by constructor, parameter based constructor is used.

From the hashcode of all the variables, we have confirmed that all the spring beans are different objects and not referring to the same object.

Since we removed “employee1” from the list of eligible beans for autowiring, there was no confusion in the bean mapping. If we remove autowire-candidate="false" from the “employee1” definition, we will get below error message when executing the above main method.

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeServiceByType' defined in class path resource [spring.xml]: Unsatisfied dependency expressed through bean property 'employee': : No qualifying bean of type [com.journaldev.spring.autowiring.model.Employee] is defined: expected single matching bean but found 2: employee,employee1; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.journaldev.spring.autowiring.model.Employee] is defined: expected single matching bean but found 2: employee,employee1
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1278)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1170)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at com.journaldev.spring.autowiring.main.SpringMain.main(SpringMain.java:12)
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.journaldev.spring.autowiring.model.Employee] is defined: expected single matching bean but found 2: employee,employee1
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1263)
	... 13 more

That’s all for the Spring @Autowired Annotation and Spring autowiring feature, please download the example project from below link and analyse it to learn more.


  1. Raghu Ram says:

    Hi Pankaj,

    Thank you for the tutorial. I have little confusion i.e if I have 2 beans in .xml file with names “employee” and “employee1” and as per employeeAutowiredByTypeService bean, It would create a bean of EmployeeAutowiredByTypeService.class till this part I am clear. Once you are getting this employeeAutowiredByTypeService obj and calling for “autowiredByTypeService.getEmployee().getName()”, then in such case which object would come because both “employee” and “employee1” are of type “Employee” how to solve this kind of collision, should we use @Qualifier(“nameOfBean”) annotation or by default does it pick up anyone? Please help me with this query.

    1. Pankaj says:

      Use @Qualifier in this case.

  2. pankaj says:

    Description Resource Location Path Type
    ClassPathXmlApplicationContext cannot be resolved to a type SpringMain.java line 13 /SpringBeanAutowiring/src/main/java/com/journaldev/spring/autowiring/main Java Problem

    I am not able to find issue . please check

  3. andy says:

    I hope I can get answer. I’m trying to create multiple object in bean configuration but it throws me an error

    It throws an exception of a conflict. But it doesn’t give any error when I instantiate random named service objects

    autowiredByConstructorService = ctx.getBean(“employeeAutowiredByConstructorService”,EmployeeAutowiredByConstructorService.class);


  4. Abhishek says:

    Hi when your page reload it ask for allow for show notification. I want to implement the same on my website. My website is built on spring and jsp. Please help me to do.

  5. Rohit says:

    Above statement is wrong. It actually call the setter method. So if bean name is employee, it will call setEmployee method. Even if you change the property name to emp, it will work.

    Usually we make sure property name and bean name is same. Then just follow the naming convention of setter method. So it works and give impression that property name and bean name should be same.

  6. SprinfDev says:

    If you write the configure your beans via javaConfig.java, instead of using .xml file that would be better.

  7. ramesh says:

    Its nice explanation.

  8. bala says:

    clean explanation sir

  9. Binh Nguyen says:

    Thanks, nice post

  10. Lalu says:

    how can i integrate webserices with spring framework

  11. ayodhya says:

    can you please give me example where and when to use autowiring in springmvc

    1. Arun Nagar says:

      Inject Dependency either through autowiring(implicitly) or explicitly(by constructor-args element and parameter element) are same but difference is that in autowiring your code will be less in xml file.but not so much readable as explicitly dependency.
      if your application is large yhan you can use autowiring instead of explicitly dependency but both are same thing.

  12. raj says:

    Hi Pankaj,

    Can you please tell me how to inject a bean having HttpSession as constructor argument

    Suppose: ClassB(HttpSession session).

    public class ClassA{

    ClassB classB

  13. Pravin Pandagale says:

    Sir i have some doubt:
    1) where you have declared the property named by–employeeServiceByName
    2) where is your property of type –employeeServiceByType

  14. vikash says:

    Why 3 different instances were created. I think default scope of bean is singleton. It is confusing to me. .. Anybody …

    1. Nishant says:

      default is singleton which is right but you can create multiple instances as well. Here 3 instances were created to show we can have 3 different ways to instantiate the beans.

      Consider a scenario in which a LockOpener class provides the service of opening any locker. The LockOpener class
      relies on a KeyHelper class for opening the locker, which was injected into LockOpener. However, the design of the
      KeyHelper class involves some internal states that make it not suitable for reuse. Every time the openLock() method
      is called, a new KeyHelper instance is required. In this case, LockOpener will be a singleton.and Key Helper will have different instances called prototype

  15. Marina says:

    Thank you for the article!

  16. Mohit Khandelwal says:

    Thank u soo much… 馃檪

  17. gaurav maheshwari says:

    If you are using @Autowired annotation and there are two same type beans declared then, first it will look byType and it will find two beans of same type but if out of those two beans one has same name or we can say one bean qualify with byName then it will work as byName.

Leave a Reply

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

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages