JavaHow To Use the @Autowired Annotation in Spring

How To Use the @Autowired Annotation in Spring content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

The Spring framework is a popular choice for Java developers due to its combination of power and simplicity. Spring is many things, but at its core, Spring is an IOC (Inversion of Control) container. It lets developers build applications that express object dependencies through configuration files. This enables you to, for example, switch implementations of a Java interface without modifying code. To achieve this, Spring developers configure dependencies in an ‘applicationContext.xml’ file. With newer releases of Spring, developers can utilize annotations, if they prefer, to configure their applications. In this article, I will demonstrate how to use the ‘@Autowired’ annotation in Spring.

Spring has always provided a mechanism for autowiring dependencies, either by type or by name. As an example, Spring can scan its configuration file at runtime, looking for a class the same type as a property found on your main object. Once found, it creates an instance of that type and then injects that instance into the object’s property. It has been a subject for debate as to whether it is a good idea to let Spring assume this responsibility. The traditional approach has been to wire beans manually, using the ref keyword in a configuration file:

<bean id="empDao" class="EmpDao" />

<bean id="empManager" class="EmpManager">
   <property name="empDao" ref="empDao" />

Here, a bean of the ‘EmpDao’ type is injected into a bean of the ‘EmpManager’ type, assuming there is a property and setter method in the EmpManager class:

public class EmpManager {

   private EmpDao empDao;

   public EmpDao getEmpDao() {
      return empDao;

   public void setEmpDao(EmpDao empDao) {
      this.empDao = empDao;


By using @Autowired, you can eliminate the additional XML in your configuration file that specifies the relationship between two objects. Also, you no longer need methods to set the property in the owning class. Just include a private or protected variable and Spring will do the rest. Here’s the modified XML:

<context:annotation-config />

<bean id="empManager" class="autowiredexample.EmpManager" />
<bean id="empDao"     class="autowiredexample.EmpDao" />

And the code…

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

public class EmpManager {

   private EmpDao empDao;


@Autowired is not just for property injection, but also can be used in methods and constructors. You can potentially reduce a great deal of XML and code with this technique. Having said this, autowiring can cause problems in more complex object models. When using @Autowired, if one and only one of the type you are looking for cannot be found, an error occurs. This is an improvement over traditional autowiring which will, by default, fail silently when an exact match cannot be found. Luckily, with @Autowired, you can add a ‘Qualifier’ attribute to identify by type and name when two objects are of the same type:


As you would expect, @Autowired is not a cure-all and it might not make sense for every situation. It is easy to fall back on traditional configuration when @Autowired does not fit. However, @Autowired is a nice option for developers building Spring applications.

About the Author

Michael Klaene is a Principal Consultant at Sogeti USA. With over 11 years of IT experience, he is focused on helping clients implement enterprise solutions with J2EE and .NET technologies.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories