詳解Spring框架注解掃描開(kāi)啟之配置細(xì)節(jié)
前言
Spring框架對(duì)Bean進(jìn)行裝配提供了很靈活的方式,下面歸納一下主要的方式:
- 在XML中進(jìn)行顯示配置
- 在Java中進(jìn)行顯示配置
- 隱式的bean發(fā)現(xiàn)機(jī)制和自動(dòng)裝配
而自動(dòng)裝配實(shí)現(xiàn)就需要注解掃描,這時(shí)發(fā)現(xiàn)了兩種開(kāi)啟注解掃描的方式,即<context:annotation-config/>和<context:component-scan>
下面歸納一下這兩種方式的異同點(diǎn):
<context:annotation-config>:注解掃描是針對(duì)已經(jīng)在Spring容器里注冊(cè)過(guò)的Bean
<context:component-scan>:不僅具備<context:annotation-config>的所有功能,還可以在指定的package下面掃描對(duì)應(yīng)的bean
Demo:
Demo:XML注冊(cè)Bean方式
下面給出兩個(gè)類(lèi),類(lèi)A和類(lèi)B
package com.test;
pubic class B{
public B(){
System.out.println("B類(lèi)");
}
}
package com.test;
public class A {
private B bClass;
public void setBClass(B bClass){
this.bClass = bClass;
System.out.println("通過(guò)set的方式注入B類(lèi)");
}
public A(){
System.out.println("A類(lèi)");
}
}
如何我們這時(shí)可以通過(guò)傳統(tǒng)的xml配置在Application.xml里進(jìn)行bean注冊(cè)
<bean id="bBean"class="com.test.B"/> <bean id="aBean"class="com.test.A"> <property name="bClass" ref="bBean"/> </bean>
啟動(dòng)加載Application.xml
輸出:
類(lèi)B
類(lèi)A
通過(guò)set的方式注入B類(lèi)
Demo:annotation配置注解開(kāi)啟方式
package com.test;
pubic class B{
public B(){
System.out.println("B類(lèi)");
}
}
package com.test;
public class A {
private B bClass;
@Autowired
public void setBClass(B bClass){
this.bClass = bClass;
System.out.println("通過(guò)set的方式注入B類(lèi)");
}
public A(){
System.out.println("A類(lèi)");
}
}
然后我們需要在Application.xml里注冊(cè)Bean,假如我們先這樣配置,僅僅注冊(cè)Bean,不開(kāi)啟掃描
<bean id="bBean"class="com.test.B"/> <bean id="aBean"class="com.test.A"/>
或者僅僅開(kāi)啟掃描,不注冊(cè)Bean
<context:annotation-config/>
這時(shí)加載Application.xml配置
輸出:
類(lèi)B
類(lèi)A
我們會(huì)發(fā)現(xiàn)下面的@Autowired的方法是不能被加載的
@Autowired
public void setBClass(B bClass){
this.bClass = bClass;
System.out.println("通過(guò)set的方式注入B類(lèi)");
}
解決方法:
修改Application.xml配置文件
<context:annotation-config/> <bean id="bBean"class="com.test.B"/> <bean id="aBean"class="com.test.A"/>
重新加載配置文件,輸出正常了
輸出:
類(lèi)B
類(lèi)A
通過(guò)set的方式注入B類(lèi)
歸納:<context:annotation-config>:注解掃描是針對(duì)已經(jīng)在Spring容器里注冊(cè)過(guò)的Bean
Demo:component配置注解開(kāi)啟方式
package com.test;
pubic class B{
public B(){
System.out.println("B類(lèi)");
}
}
package com.test;
@Component
public class A {
private B bClass;
@Autowired
public void setBClass(B bClass){
this.bClass = bClass;
System.out.println("通過(guò)set的方式注入B類(lèi)");
}
public A(){
System.out.println("A類(lèi)");
}
}
然后我們配置一下application.xml,開(kāi)啟annotaion-config掃描
<context:annotation-config />
加載配置文件:
輸出:
類(lèi)B
類(lèi)A
原因:<context:annotation-config>:注解掃描是針對(duì)已經(jīng)在Spring容器里注冊(cè)過(guò)的Bean,Bean并沒(méi)有注冊(cè)過(guò),所以即使開(kāi)啟了@Autowired、@Component注解 和配置開(kāi)啟了annotaion-config掃描還是加載不到
修改配置文件:
<context:component-scan base-package="com.test"/>
重新加載配置文件:
輸出:
類(lèi)B
類(lèi)A
通過(guò)set的方式注入B類(lèi)
歸納:
<context:annotation-config>:注解掃描是針對(duì)已經(jīng)在Spring容器里注冊(cè)過(guò)的Bean
<context:component-scan>:不僅具備<context:annotation-config>的所有功能,還可以在指定的package下面掃描對(duì)應(yīng)的bean
<context:annotation-config />和 <context:component-scan>同時(shí)存在的時(shí)候,前者會(huì)被忽略。
即使注冊(cè)Bean,同時(shí)開(kāi)啟<context:annotation-config />掃描,@autowire,@resource等注入注解只會(huì)被注入一次,也即只加載一次
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
分布式醫(yī)療掛號(hào)系統(tǒng)SpringCache與Redis為數(shù)據(jù)字典添加緩存
這篇文章主要為大家介紹了分布式醫(yī)療掛號(hào)系統(tǒng)SpringCache與Redis為數(shù)據(jù)字典添加緩存,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
使用Netty快速實(shí)現(xiàn)一個(gè)群聊功能的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用?Netty?框架開(kāi)發(fā)一個(gè)?WebSocket?服務(wù)端,從而實(shí)現(xiàn)一個(gè)簡(jiǎn)單的在線聊天功能,感興趣的小伙伴可以了解下2023-11-11
SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Druid簡(jiǎn)單實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪改查方式
這篇文章主要介紹了Druid簡(jiǎn)單實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪改查方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Spring MVC訪問(wèn)靜態(tài)文件_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Spring MVC訪問(wèn)靜態(tài)文件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
使用迭代器Iterator遍歷Collection問(wèn)題
這篇文章主要介紹了使用迭代器Iterator遍歷Collection問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

