The constructor injection

The constructor injection, on the other hand, uses a constructor to inject dependencies. The following code shows how to use a constructor for injecting in DataService:

    public class BusinessServiceImpl {       private DataService dataService;       @Autowired       public BusinessServiceImpl(DataService dataService) {         super();         this.dataService = dataService;       }     }

When you run the code with the preceding implementation of BusinessServiceImpl, you will see this statement in the log, asserting that autowiring took place using the constructor:

    Autowiring by type from bean name 'businessServiceImpl' via     constructor to bean named 'dataServiceImpl'

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.