标签:com 直接 reference count 注意 single end autowire enc
原课程:通过注解注入Bean
Spring 4.x推荐使用基于构造器的方式进行bean注入7.4.1 Dependency Injection
spring为什么推荐使用构造器注入
注意:通过构造器注入Bean时,如果只有一个构造器,可以不写@Autowired注解,详见17. Spring Beans and Dependency Injection
The following example shows a @Service Bean that uses constructor injection to obtain a required RiskAssessor bean:
package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;
@Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}
If a bean has one constructor, you can omit the @Autowired, as shown in the following example:
@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}
实例化和注入时指定Bean的ID
直接注入List实例
将多个泛型实例注入到List
直接注入Map实例
将多个泛型实例注入到Map
标签:com 直接 reference count 注意 single end autowire enc
原文地址:https://www.cnblogs.com/z00377750/p/11961668.html