Spring @Autowired Annotation With Examples

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

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.


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.

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.

Spring @Autowired Annotation – Service Class

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

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.

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.

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.

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.

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.

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.

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.

By admin

Leave a Reply