Spring多对象引入方法
在以前使用xml配置注入的时候,可以通过name名称注入,也可以使用type类型注入.
在SpringBoot中,可以使用@Resource和@Autowried注解进行注入.
@Resource默认会使用名称进行注入, 如果找不到,会使用自动使用类型进行注入.
@Autowried,则会在容器中寻找匹配的对象,如果找到则注入成功,如果没找到或者找到多个,则会报错.
但是,如果有多个的情况下,可以使用@Qualifier("别名")进行约束.
1.创建Bean
1.1如果使用方法bean注入对象,给@Bean添加name值.
@Bean(name="别名")
代码样例:
注意
下面的@Bean(name="test01DataSource"),可以使用applicationContext.getBean("test01DataSource")进行获取.
@Primary表示如果以type类型进行选择的话,首选该Bean.
也即是说,使用applicationContext.getBean(DataSource.class)和applicationContext.getBean("test01DataSource")获取到的是同一个对象.
源码:
@Configuration
publicclassDataSourceConfig{
@Bean(name="test01DataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.test01")
publicDataSourcegetTest01DataSource(){
returnDataSourceBuilder.create().build();
}
@Bean(name="test02DataSource")
@ConfigurationProperties(prefix="spring.datasource.test02")
publicDataSourcetest02DataSource(){
returnDataSourceBuilder.create().build();
}
}
测试代码:
@Autowired
ApplicationContextapplicationContext;
@Test
publicvoidtestDataSourceHashCode(){
System.out.println(applicationContext.getBean(DataSource.class).hashCode());
System.out.println((applicationContext.getBean("test01DataSource")).hashCode());
System.out.println((applicationContext.getBean("test02DataSource")).hashCode());
}
结果样例:
1105282397
1105282397
793657559
1.2使用类注解进行注入对象.@Service,@Component,添加value值进行创建别名.
//创建接口
publicinterfaceIBeanService{
publicvoidprintInfo();
}
//创建实例01,并且添加上@Primary注解,表名默认使用这个类
@Service(value="bean01")
@Primary
publicclassBean01ServiceimplementsIBeanService{
@Override
publicvoidprintInfo(){
System.out.println("Thisisbean01");
}
}
//创建实例02,
@Service(value="bean02")
publicclassBean02ServiceimplementsIBeanService{
@Override
publicvoidprintInfo(){
System.out.println("Thisisbean02");
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
publicclassIBeanServiceTest{
@Autowired
ApplicationContextapplicationContext;
//createdefaultbean.
@Autowired
IBeanServicebeanService;
@Autowired
@Qualifier("bean01")
IBeanServicebean01Service;
@Autowired
@Qualifier("bean02")
IBeanServicebean02Service;
@Test
publicvoidprintInfo(){
//createbean01
IBeanServicebean01=(IBeanService)applicationContext.getBean("bean01");
//createbean02
IBeanServicebean02=(IBeanService)applicationContext.getBean("bean02");
bean01.printInfo();
bean02.printInfo();
//createdefaultbean,anditisbean01
applicationContext.getBean(IBeanService.class).printInfo();
}
@Test
publicvoidprintInfoThroughAutowired(){
beanService.printInfo();
bean01Service.printInfo();
bean02Service.printInfo();
}
}
2.调用
2.1.如果需要创建局部变量, 使用applicationContext.getBean(class/name)创建
对于有@Primary的对象,直接使用getBean(class)进行调用.
applicationContext.getBean(IBeanService.class)
对于其他的Bean,使用getBean(name)进行调用,并进行类型强制转换.
eg.(IBeanService)applicationContext.getBean("bean02");
2.2.如果需要创建成员变量,使用@Autowired和@Qualifier("别名")进行
对于有@Primary的对象,直接使用@Autowired进行调用.代码如下
@Autowired IBeanServicebeanService;
对于其他的bean,通过添加@Qualifier("别名")进行调用,代码如下
@Autowired
@Qualifier("bean02")
IBeanServicebean02Service;
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接