spring組件掃描使用詳解 (

關於spring自動檢測組件的使用方式網上太多了,而且也不是我記錄的重點,我想說下一點可能你還不知道的經驗
我們知道如果不想在xml文件中配置bean,我們可以給我們的類加上spring組件註解,只需再配置下spring的掃描器就可以實現bean的自動載入。
 
先寫一個小例子,剩下的在下面解釋
<!-- 定義掃描根路徑爲leot.test,不使用默認的掃描方式 -->
<context:component-scan base-package="leot.test" use-default-filters="false">
  <!-- 掃描符合@Service @Repository的類 -->
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
 
下面是引用spring framework開發手冊中的一段話

Spring 2.5引入了更多典型化註解(stereotype annotations): @Component@Service@Controller@Component 是所有受Spring管理組件的通用形式;而@Repository@Service@Controller 則是@Component 的細化,用來表示更具體的用例(例如,分別對應了持久化層、服務層和表現層)。也就是說,你能用@Component 來註解你的組件類,但如果用@Repository@Service@Controller 來註解它們,你的類也許能更好地被工具處理,或與切面進行關聯。例如,這些典型化註解可以成爲理想的切入點目標。當然,在Spring Framework以後的版本中, @Repository@Service@Controller 也許還能攜帶更多語義。如此一來,如果你正在考慮服務層中是該用 @Component 還是@Service ,那@Service 顯然是更好的選擇。同樣的,就像前面說的那樣, @Repository 已經能在持久化層中進行異常轉換時被作爲標記使用了。”

 

下面是網上目前關於組件掃描最詳細的介紹

Spring applicationContext.xml的<context:component-scan>標籤用途比我想像的還要實用。而且後來才 知道,有了<context:component-scan>,另一個<context:annotation- config/>標籤根本可以移除掉,因為被包含進去了。原本我survery Spring3通常只配置成<context:component-scan base-package="com.foo.bar"/>,意即在base-package下尋找有@Component和 @Configuration的target Class。而現在如下的飯粒:

<context:component-scan base-package="com.foo" use-default-filters ="false">
<context:include-filter type="regex" expression="com.foo.bar.*Config"/>
<context:include-filter type="regex" expression="com.foo.config.*"/>
</context:component-scan>

  <context:component-scan>提供兩個子標籤:<context:include-filter> 和<context:exclude-filter>各代表引入和排除的過濾。而上例把use-default-filters屬性設為 false,意即在base-package所有被宣告為@Component和@Configuration等target Class不予註冊為bean,由filter子標籤代勞。

  filter標籤在Spring3有五個type,如下:

Filter Type Examples Expression Description
annotation org.example.SomeAnnotation 符合SomeAnnoation的target class
assignable org.example.SomeClass 指定class或interface的全名
aspectj org.example..*Service+ AspectJ語法
regex org\.example\.Default.* Regelar Expression
custom org.example.MyTypeFilter Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter

  所以上例用的regex就有個語病,com.foo.config.* 可以找到com.foo.config.WebLogger,但也可以找到com1fool2config3abcde,因為小數點在Regex是任意字 元,是故要用\.把小數點跳脫為佳。(2010/3/15補充:但要使用\.方式,其use-default-filters不能為false,否則抓不 到,感覺是Bug)

  Spring3提供豐富的Filter支援,有益配置策略,不需面臨Configuration Hell,比如Regex的com\.foo\.*\.action\.*Config,這樣就可以找到com.foo package下所有action子package的*Config的target class。

 

我按他的例子,配置了我自己的如下:

<context:component-scan base-package="com.xhlx.finance.budget"  >
  <context:include-filter type="regex" expression="com.lee.finance.budget.service.*"/>
  <context:include-filter type="regex" expression="com.lee.finance.budget.security.*"/> 
</context:component-scan>
但是死活掃描不到,網上又沒有更好的講解,沒辦法只好自己試,改成
<context:component-scan base-package="com.xhlx.finance.budget" >
  <context:include-filter type="regex" expression="com.lee.finance.budget.*"/>
</context:component-scan>
這樣連沒有加註解的類也掃描並實例化,結果報錯,因爲有的類根本就沒有默認的構造函數不能實例化,能不報錯嗎
改成
<context:component-scan base-package="com.xhlx.finance.budget"  >
  <context:include-filter type="regex" expression="com\.lee\.finance\.budget\.service.*"/>  
</context:component-scan>問題依舊
<context:component-scan base-package="com.xhlx.finance.budget" >
  <context:include-filter type="regex" expression="com.lee.finance.budget.service.TestService"/>
</context:component-scan>
嘿,這次可以了,寫全限定名就可以,表達式卻不行,但是如果我有成千上百個還得一個一個這樣配置啊?不行,繼續研究,我想有個base-package的配置,從表面意思來看這是個“基本包”,是否表示下面的過濾路徑是基於這個包的呢?於是試着改成
<context:component-scan base-package="com.xhlx.finance.budget" >
  <context:include-filter type="regex" expression=".service.*"/>
</context:component-scan>
嘿,行了。 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章