Autowired Annotation in Spring

@Autowired annotation is a relatively new style of implementing a Dependency Injection. It allows you to inject other beans into another desired bean. Similar to the @Required annotation, the @Autowired annotation can be used to “autowire” bean on setter methods as well as constructors and properties..

java-featured-image

@Autowired Annotation on Setter Methods

Please note that when the @Autowired annotation is used on a setter method, it automatically gets rids of the <property> element that resides in the XML configuration file. Instead, when Spring discovers a setter method that has used the @Autowired annotation, it performs a byType “autowiring” on that specific method.

Let’s see @Autowired in practice. More specifically, @Autowired on a setter method.

ExampleService.java

public class ExampleService {
    private Employee employee;

    @Autowired
    public void setEmployee(Employee emp) {
        // setting the employee
        employee = emp;
    }
}

Breakdown

Nothing fancy in the example above. We just have a service class named ExampleService that has an instance variable of type Employee and has a setter method named setEmployee(Employee emp) that just sets the employee to whatever is given as an argument.

Thanks to the @Autowired annotation, an instance of Employee is being injected as an argument to that method when ExampleService is instantiated.



@Autowired Annotation on Constructors

public class ExampleService {
    private Employee employee;

    @Autowired
    public ExampleService(Employee employee) {
        this.employee = employee;
    }

     @Autowired
     public void setEmployee(Employee emp) {
         // setting the employee
         employee = emp;
     }
}

Once again, thanks to the @Autowired annotation, an instance of Employee is being injected as an argument to the constructor when the ExampleService is instantiated.

@Autowired Annotation on Properties

Autowiring properties saves us time and code lines. How? Well, when we use that annotation on Properties, these properties no longer need getters and setters. Cool right?

public class ExampleService {
    @Autowired
    private Employee employee;

     @Autowired
     public ExampleService(Employee employee) {
         this.employee = employee;
     }

     @Autowired
     public void setEmployee(Employee emp) {
         // setting the employee
         employee = emp;
     }
}

In the code snippet above, we autowired the property named employee. Therefore, it no longer needs setter and getter methods. employee gets injected by Spring when ExampleService is created.

Optional Dependencies

@Autowired can also be optional. Like so:

public class ExampleService {
    @Autowired(required = false)
    private Employee employee;

     @Autowired
     public ExampleService(Employee employee) {
         this.employee = employee;
     }

     @Autowired
     public void setEmployee(Employee emp) {
         // setting the employee
         employee = emp;
     }
}

required=false made it not a required dependency.

The reason that we need optional dependencies is because Spring expects dependencies that are @Autowired to be available when the dependent bean is constructed. Otherwise, it will throw an error. Thanks to required=false, we get around this issue.

Summary

By using the @Autowired annotation, we save a few lines of code and also saves us some time as we don’t need to specify properties and constructor arguments.

1 1 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments