SpringBoot通過注解注入Bean的幾種方式解析
1、背景
我們談到Spring的時候一定會提到IOC容器、DI依賴注入,Spring通過將一個個類標(biāo)注為Bean的方法注入到IOC容器中,達到了控制反轉(zhuǎn)的效果。那么我們剛開始接觸Bean的時候,一定是使用xml文件,一個一個的注入,就例如下面這樣。
<bean id="bean" class="com.xxx.xxx.Bean" />
我們的項目一般很大的話,就需要成千上百個Bean去使用,這樣寫起來就很繁瑣。那么Spring就幫我們實現(xiàn)了一種通過注解來實現(xiàn)注入的方法。只需要在你需要注入的類前面加上相應(yīng)的注解,Spring就會幫助我們掃描到他們?nèi)崿F(xiàn)注入。
xml掃描包的方式
<context:component-scan base-package="com.xxx"/>
2、通過注解注入的一般形式
一般情況下,注入Bean有一個最直白,最易懂的方式去實現(xiàn)注入,下面廢話先不多說,先貼代碼。
2.1、Bean類
public class MyBean{ }
2.2、Configuration類
//創(chuàng)建一個class配置文件 @Configuration public class MyConfiguration{ //將一個Bean交由Spring進行管理 @Bean public MyBean myBean(){ return new MyBean(); } }
2.3、Test類
與xml有一點不同,這里在Test中,實例化的不再是ClassPathXmlApplicationContext,而是獲取的AnnotationConfigApplicationContext實例。
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); MyBean myBean = cotext.getBean("myBean",MyBean.class); System.out.println("myBean = " + myBean);
上面的代碼中MyBean也就是我們需要Spring去管理的一個Bean,他只是一個簡單的類。而MyConfiguration中,我們首先用@Configuration注解去標(biāo)記了該類,這樣標(biāo)明該類是一個Spring的一個配置類,在加載配置的時候會去加載他。
在MyConfiguration中我們可以看到有一個方法返回的是一個MyBean的實例,并且該方法上標(biāo)注著@Bean的注解,標(biāo)明這是一個注入Bean的方法,會將下面的返回的Bean注入IOC。
3、通過構(gòu)造方法注入Bean
我們在生成一個Bean實例的時候,可以使用Bean的構(gòu)造方法將Bean實現(xiàn)注入。直接看代碼
3.1、Bean類
@Component public class MyBeanConstructor { private AnotherBean anotherBeanConstructor; @Autowired public MyBeanConstructor(AnotherBean anotherBeanConstructor){ this.anotherBeanConstructor = anotherBeanConstructor; } @Override public String toString() { return "MyBean{" + "anotherBeanConstructor=" + anotherBeanConstructor + '}'; } }
3.2、AnotherBean類
@Component(value="Bean的id,默認(rèn)為類名小駝峰") public class AnotherBean { }
3.3、Configuration類
@Configuration @ComponentScan("com.company.annotationbean") public class MyConfiguration{ }
這里我們可以發(fā)現(xiàn),和一般方式注入的代碼不一樣了,我們來看看新的注解都是什么意思:
@AutoWired
簡單粗暴,直接翻譯過來的意思就是自動裝配:wrench:,還不理解為什么叫自動裝配:wrench:?看了下一個注解的解釋你就知道了。若是在這里注入的時候指定一個Bean的id就要使用@Qualifier注解
@Component(默認(rèn)單例模式)
什么??這翻譯過來是零件,怎么感覺像是修汽車??是的,Spring管理Bean的方法就是修汽車的方式。我們在需要將一個類變成一個Bean被Spring可以注入的時候加上注解零件@Conmonent,那么我們就可以在加載Bean的時候把他像零件一樣裝配:wrench:到這個IOC汽車上了
在這里我們還有幾個其他的注解也可以實現(xiàn)這個功能,也就是細化的@Component:
@Controller 標(biāo)注在Controller層
@Service 標(biāo)注在Service層
@Repository 標(biāo)注在dao層
@ComponentScan("")
還是翻譯,零件掃描,我們?nèi)タ纯蠢ㄌ柪锏?ldquo;零件倉庫”里面,哪些“零件”(類)需要被裝載,Spring就會去掃描這個包,將里面所有標(biāo)注了@Component的類進行注入。
這里的通過構(gòu)造方法進行注入就很好理解了,我們在裝配MyBean這個零件的時候,突然發(fā)現(xiàn)他必須在AnotherBean的基礎(chǔ)上才能安裝到IOC里面,那么我們就在每次裝配MyBean的時候自動裝配:wrench:一個AnotherBean進去。舉個:chestnut:吧:
還是以汽車為例,我們在踩油門出發(fā)之前,是不是必須發(fā)車??這里的AutoWired的內(nèi)容就像發(fā)車,你不發(fā)車,這個油門你踩斷都沒有用,他都不會走。
4、通過set方法注入Bean
我們可以在一個屬性的set方法中去將Bean實現(xiàn)注入,看代碼吧
4.1、MyBean類
@Component public class MyBeanSet { private AnotherBean anotherBeanSet; @Autowired public void setAnotherBeanSet(AnotherBean anotherBeanSet) { this.anotherBeanSet = anotherBeanSet; } @Override public String toString() { return "MyBeanSet{" + "anotherBeanSet=" + anotherBeanSet + '}'; } }
4.2、Configuration類 和 Test類
同上一個,就不貼了
這里我們發(fā)現(xiàn)在setter方法上我們有一個@AutoWired,與上面不同的是,我們不會在實例化該類時就自動裝配:wrench:這個對象,而是在顯式調(diào)用setter的時候去裝配。
5、通過屬性去注入Bean
我們前面兩種注入的方式諸如時間不同,并且代碼較多,若是通過屬性,即就是
@Component public class MyBeanProperty { @Autowired private AnotherBean anotherBeanProperty; @Override public String toString() { return "MyBeanProperty{" + "anotherBeanProperty=" + anotherBeanProperty + '}'; } }
這里我們可以看到我們這個類中需要使用AnotherBean這個實例對象,我們可以通過@AutoWired去自動裝配它。
6、通過List注入Bean
6.1、MyBeanList類
@Component public class MyBeanList { private List<String> stringList; @Autowired public void setStringList(List<String> stringList) { this.stringList = stringList; } public List<String> getStringList() { return stringList; } }
6.2、MyConfiguration類
@Configuration @ComponentScan("annoBean.annotationbean") public class MyConfiguration { @Bean public List<String> stringList(){ List<String> stringList = new ArrayList<String>(); stringList.add("List-1"); stringList.add("List-2"); return stringList; } }
這里我們將MyBeanList進行了注入,對List中的元素會逐一注入。下面介紹另一種方式注入List
6.3、MyConfiguration類
@Bean //通過該注解設(shè)定Bean注入的優(yōu)先級,不一定連續(xù)數(shù)字 @Order(34) public String string1(){ return "String-1"; } @Bean @Order(14) public String string2(){ return "String-2"; }
注入與List中泛型一樣的類型,會自動去匹配類型,及時這里沒有任何List的感覺,只是String的類型,但他會去通過List的Bean的方式去注入。
第二種方式的優(yōu)先級高于第一種,當(dāng)兩個都存在的時候,若要強制去使用第一種方式,則要去指定Bean的id即可
7、通過Map去注入Bean
@Component public class MyBeanMap { private Map<String,Integer> integerMap; public Map<String, Integer> getIntegerMap() { return integerMap; } @Autowired public void setIntegerMap(Map<String, Integer> integerMap) { this.integerMap = integerMap; } } @Bean public Map<String,Integer> integerMap(){ Map<String,Integer> integerMap = new HashMap<String, Integer>(); integerMap.put("map-1",1); integerMap.put("map-2",2); return integerMap; } @Bean public Integer integer1(){ return 1; } @Bean public Integer integer2(){ return 2; }
同樣這里也具有兩種方式去注入Map類型Bean,且第二種的優(yōu)先值高于第一種
以上就是Bean通過注解注入的幾種方式,大家可以對比著xml注入的方式去看。
希望大家以后多多支持腳本之家!
相關(guān)文章
springboot詳解實現(xiàn)車險理賠信息管理系統(tǒng)代碼
本系統(tǒng)基于Springboot開發(fā)實現(xiàn)了一個為用戶車險進行理賠信息管理的一個信息化管理系統(tǒng),核心的業(yè)務(wù)主要是用戶申請保險理賠,管理員審核進入理賠程序,事故調(diào)查員對事故進行調(diào)查和現(xiàn)場勘察,這其中共涉及到三類用戶,購買保險的客戶,事故調(diào)查員和系統(tǒng)管理員2022-06-06springboot工程jar包部署到云服務(wù)器的方法
這篇文章主要介紹了springboot工程jar包部署到云服務(wù)器的方法,本文通過實例介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧2018-05-05java基于mongodb實現(xiàn)分布式鎖的示例代碼
本文主要介紹了java基于mongodb實現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08Java Integer[]和int[]互相轉(zhuǎn)換方式
這篇文章主要介紹了Java Integer[]和int[]互相轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12spring?boot整合mongo查詢converter異常排查記錄
這篇文章主要為大家介紹了spring?boot整合mongo查詢時拋出converter異常的排查解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03SpringBoot中使用JdbcTemplate訪問Oracle數(shù)據(jù)庫的案例詳解
JdbcTemplate是Spring框架中的一個核心類,用于簡化Java應(yīng)用程序與關(guān)系型數(shù)據(jù)庫的交互操作,本文給大家介紹SpringBoot中使用JdbcTemplate訪問Oracle數(shù)據(jù)庫的方法,感興趣的朋友跟隨小編一起看看吧2023-10-10