Dependency injection

To wire up dependencies, we can use these two annotations provided by Spring—the @Required annotation and the @Autowired annotation. We can apply the @Required annotation to setter methods and the @Autowired annotation to constructor, methods, as well as fields. 

To sum up, in Spring, there are three ways to inject dependencies:

  • Constructor-based injection
  • Setter-based/method-based injection
  • Field-based injection.