Spring多對象引入方法
在以前使用xml配置注入的時候, 可以通過name名稱注入, 也可以使用type類型注入.
在SpringBoot中, 可以使用@Resource和@Autowried注解進(jìn)行注入.
@Resource 默認(rèn)會使用名稱進(jìn)行注入, 如果找不到, 會使用自動使用類型進(jìn)行注入.
@Autowried, 則會在容器中尋找匹配的對象, 如果找到則注入成功, 如果沒找到或者找到多個, 則會報錯.
但是, 如果有多個的情況下, 可以使用@Qualifier("別名") 進(jìn)行約束.
1.創(chuàng)建Bean
1.1如果使用方法bean注入對象, 給@Bean添加name值.
@Bean(name="別名")
代碼樣例:
注意
下面的@Bean(name = "test01DataSource"), 可以使用applicationContext.getBean("test01DataSource") 進(jìn)行獲取.
@Primary表示 如果以type類型進(jìn)行選擇的話, 首選該Bean.
也即是說,使用applicationContext.getBean(DataSource.class) 和applicationContext.getBean("test01DataSource") 獲取到的是同一個對象.
源碼:
@Configuration
public class DataSourceConfig {
@Bean(name = "test01DataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.test01")
public DataSource getTest01DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test02DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test02")
public DataSource test02DataSource() {
return DataSourceBuilder.create().build();
}
}
測試代碼:
@Autowired
ApplicationContext applicationContext;
@Test
public void testDataSourceHashCode() {
System.out.println(applicationContext.getBean(DataSource.class).hashCode());
System.out.println((applicationContext.getBean("test01DataSource")).hashCode());
System.out.println((applicationContext.getBean("test02DataSource")).hashCode());
}
結(jié)果樣例:
1105282397
1105282397
793657559
1.2使用類注解進(jìn)行注入對象. @Service, @Component,添加value值進(jìn)行創(chuàng)建別名.
// 創(chuàng)建接口
public interface IBeanService {
public void printInfo();
}
//創(chuàng)建實(shí)例01, 并且添加上@Primary注解, 表名默認(rèn)使用這個類
@Service(value = "bean01")
@Primary
public class Bean01Service implements IBeanService {
@Override
public void printInfo() {
System.out.println("This is bean 01");
}
}
//創(chuàng)建實(shí)例02,
@Service(value = "bean02")
public class Bean02Service implements IBeanService {
@Override
public void printInfo() {
System.out.println("This is bean 02");
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class IBeanServiceTest {
@Autowired
ApplicationContext applicationContext;
// create default bean.
@Autowired
IBeanService beanService;
@Autowired
@Qualifier("bean01")
IBeanService bean01Service;
@Autowired
@Qualifier("bean02")
IBeanService bean02Service;
@Test
public void printInfo() {
// create bean01
IBeanService bean01 = (IBeanService) applicationContext.getBean("bean01");
// create bean02
IBeanService bean02 = (IBeanService) applicationContext.getBean("bean02");
bean01.printInfo();
bean02.printInfo();
// create default bean, and it is bean01
applicationContext.getBean(IBeanService.class).printInfo();
}
@Test
public void printInfoThroughAutowired() {
beanService.printInfo();
bean01Service.printInfo();
bean02Service.printInfo();
}
}
2.調(diào)用
2.1.如果需要創(chuàng)建局部變量, 使用applicationContext.getBean(class/name)創(chuàng)建
對于有@Primary的對象, 直接使用getBean(class)進(jìn)行調(diào)用.
applicationContext.getBean(IBeanService.class)
對于其他的Bean, 使用getBean(name)進(jìn)行調(diào)用, 并進(jìn)行類型強(qiáng)制轉(zhuǎn)換.
eg. (IBeanService) applicationContext.getBean("bean02");
2.2.如果需要創(chuàng)建成員變量, 使用@Autowired和 @Qualifier("別名") 進(jìn)行
對于有@Primary的對象, 直接使用@Autowired進(jìn)行調(diào)用.代碼如下
@Autowired IBeanService beanService;
對于其他的bean, 通過添加@Qualifier("別名")進(jìn)行調(diào)用, 代碼如下
@Autowired
@Qualifier("bean02")
IBeanService bean02Service;
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
使用JSON.toJSONString格式化成json字符串時保留null屬性
這篇文章主要介紹了使用JSON.toJSONString格式化成json字符串時保留null屬性,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
springboot支持https請求的實(shí)現(xiàn)
本文主要介紹了springboot支持https請求的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
bootstrap實(shí)現(xiàn)多個下拉框同時搜索的實(shí)例
下面小編就為大家?guī)硪黄猙ootstrap實(shí)現(xiàn)多個下拉框同時搜索的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
SpringBoot如何IDEA中實(shí)現(xiàn)熱部署
這篇文章主要介紹了SpringBoot如何IDEA中實(shí)現(xiàn)熱部署,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04

