Alberto A. Flores

Alberto A. Flores

Sr. Software Engineer

© 2022

Dark Mode

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 enhancement that, while minor, may save some from undesirable behavior. This is available since the 4.3 Spring release and it’s called “Implicit Construction Injection”.

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

1
2
3
4
5
6
7
8
9
10
11
12
@Component
public class TaskManager {
  private ServiceReviewPublisher publisher;

 /**
  * This constructor will need dependency injection
  */
  @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 Spring 4.3, that is no longer required. This will make the new code be this way:

1
2
3
4
5
6
7
8
9
10
11
@Component
public class TaskManager {
  private ServiceReviewPublisher publisher;

 /**
  * This constructor have dependency injection because of the @Component stereotype.
  */
  public TaskManager(ServiceReviewPublisher publisher) {
    this.publisher = publisher;
  }
}

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