Tuesday, December 3, 2019

Spring: Implicit Construction Injection

Over the years, Spring has made a number of enhancements to improve the developer UX. I realize some of these can be too trivial to notice. Today, I present one available since the 4.3 release (over 2 years ago).

As you know, constructor injection is a preferred pattern to add code to your dependency definition. For example, we can do the following:

 @Component
 public class TaskManager {
    private ServiceReviewPublisher publisher;

    @Autowired
    public TaskManager(ServiceReviewPublisher publisher) {
      this.publisher = publisher;
    }
 }

Notice that historically, developers would have to declare the constructor with an @Autowired annotation. This was so that the code can do proper injection. As of 4.3, that is no longer required. This will make the new code be this way:


 @Component
 public class TaskManager {
    private ServiceReviewPublisher publisher;

    public TaskManager(ServiceReviewPublisher publisher) {
      this.publisher = publisher;
    }
 }

This makes this class one step closer to be a POJO.

No comments: