上QQ阅读APP看书,第一时间看更新
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'