本章节将通过样例的方式展示@Autowired的注入的几种形式以及使用场景
public interface DemoDao {
public void haha(String a);
}
@Component
@Primary
public class DemoDaoImpl1 implements DemoDao {
@Override
public void haha(String a) {
System.err.println("print:"+a);
}
}
@Component
public class DemoDaoImpl2 implements DemoDao {
@Override
public void haha(String a) {
System.err.println("print:"+a);
}
}
特别说明:当前DemoDao有两个实现,如果有多个实现且未指定@Primary情况下,则在注入单个属性的时候会报错,错误信息如下,因为已经给DemoDaoImpl1指定了@Primary则不会报错
No qualifying bean of type ‘com.refile.registar.DemoDao’ available: expected single matching bean but found 2: demoDaoImpl1,demoDaoImpl2
@Component
public class AutowiredDemo {
// 自动注入所有实现DemoDao接口的实现Bean,key为BeanName,value为bean的实例
@Autowired
Map maps;
// 自动注入所有实现DemoDao接口的实现Bean
@Autowired
DemoDao[] demoDaos;
// 自动注入所有实现DemoDao接口的实现Bean
@Autowired
List demoDaoList;
// 通过构造器注入
DemoDao demoDaoByConstructor;
// 通过构造器注入的时候,该构造方法上可加@Autowired,也可以不加
// 可在此方法内部加断点(可看到进入)
public AutowiredDemo(DemoDao demoDao){
this.demoDaoByConstructor = demoDao;
}
// 通过自定义方法注入,方法名随意,可以是setter方法
DemoDao demoDaoByCustomMethod;
// 可在此方法内部加断点(可看到进入)
@Autowired
public void byCustomMethod(DemoDao demoDao) {
this.demoDaoByCustomMethod = demoDao;
}
// 通过参数注入,这种方式不可以,因为Spring内部没有对参数上的@Autowired进行解析
DemoDao demoDaoByParameter;
// 可在此方法内部加断点(无法进入)
public void xx(@Autowired DemoDao demoDao){
this.demoDaoByParameter = demoDao;
}
}
可以在构造方法、自定义方法、参数注入的方法上
public class MainTest {
@SuppressWarnings("resource")
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(DemoDaoImpl1.class, DemoDaoImpl2.class, AutowiredDemo.class);
AutowiredDemo demo = (AutowiredDemo) applicationContext.getBean("autowiredDemo");
System.err.println("结束");
}
}
// 使用构造器注入一个单实例Bean
@Component
public class AutowiredDemo {
private final DemoDao demoDao;
public AutowiredDemo(DemoDao demoDao){
this.demoDao = demoDao;
}
}
// 使用构造器注入一个单实例Bean
@Component
public class AutowiredDemo {
@Autowired
private DemoDao demoDao;
}
// 使用构造器同时注入多个单实例Bean
@Component
public class AutowiredDemo {
private final DemoDao demoDao;
private final DemoMapper demoMapper;
public AutowiredDemo(DemoDao demoDao, DemoMapper demoMapper){
this.demoDao = demoDao;
this.demoMapper = demoMapper;
}
}
// 使用构造器注入一个单实例Bean
@Component
public class AutowiredDemo {
@Autowired
private DemoDao demoDao;
@Autowired
private DemoMapper demoMapper;
}
根据用户的登录名,去使用不同的demoDao进行提供服务,也可以用于多租户等需要根据某些条件动态切换不同实现来提供服务的场景
// 使用构造器注入一个单实例Bean
@Component
public class AutowiredDemo {
@Autowired
private Map demoDaoMap;
public authByLoginName(String loginName){
DemoDao demoDao;
// 根据登录的用户名判断使用哪个实现
if (loginName.contain("super")){
demoDao = demoDaoMap.get("demoDaoImpl1");
} else {
demoDao = demoDaoMap.get("demoDaoImpl2");
}
...
}
}
// 使用构造器注入一个单实例Bean
public class AutowiredDemo {
private final Map demoDaoMap;
public AutowiredDemo(Map demoDaoMap){
this.demoDaoMap = demoDaoMap;
}
public authByLoginName(String loginName){
DemoDao demoDao;
if (loginName.contain("super")){
demoDao = demoDaoMap.get("demoDaoImpl1");
} else {
demoDao = demoDaoMap.get("demoDaoImpl2");
}
...
}
}