context:component-scan 的使用說明

轉載地址:https://blog.csdn.net/qwe5810658/article/details/74343228java

一般狀況下咱們在建立spring項目的時候在xml配置文件中都會配置這個標籤,配置完這個標籤後,spring就會去自動掃描base-package對應的路徑或者該路徑的子包下面的java文件,若是掃描到文件中帶有@Service,@Component,@Repository,@Controller等這些註解的類,則把這些類註冊爲bean 
注:在註解後加上例如@Component(value=」abc」)時,註冊的這個類的bean的id就是adc.正則表達式

注:若是配置了<context:component-scan>,
那麼<context:annotation-config/>標籤就能夠不用在xml中再配置了,
由於前者包含了後者。另外<context:annotation-config/>還提供了兩個子標籤 <context:include-filter><context:exclude-filter>
 

在註解注入以前也必須在spring的配置文件中作以下配置,咱們看下spring.xml文件的內容:spring

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.sparta.trans" use-default-filters="false">  

              <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>      

        </context:component-scan>

 </beans>

這個配置文件中必須聲明xmlns:context 這個xml命名空間,在schemaLocation中須要指定schema:express

http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd

 

這個文件中beans根節點下只有一個context:component-scan節點,此節點有兩個屬性base-package屬性告訴spring要掃描的包,use-default-filters=」false」表示不要使用默認的過濾器,此處的默認過濾器,會掃描包含@Service,@Component,@Repository,@Controller註解修飾的類,use-default-filters屬性的默認值爲true,這就意味着會掃描指定包下標有@Service,@Component,@Repository,@Controller的註解的所有類,並註冊成bean。 
因此若是僅僅是在配置文件中寫<context:component-scan base-package="com.sparta.trans"/> Use-default-filter此時爲true時,那麼會對base-package包或者子包下全部的java類進行掃描,並把匹配的java類註冊成bean。spa

因此這用狀況下能夠發現掃描的力度仍是挺大的,可是若是你只想掃描指定包下面的Controller,那該怎麼辦?此時子標籤<context:incluce-filter>就能夠發揮做用了。以下所示.net

context:component-scan base-package="com.sparta.trans.controller">

        <context:include-filter type="regex" expression="com\.sparta\.trans\.[^.]+(Controller|Service)"/>

                 <!--  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  -->

</context:component-scan> 

 

這樣就會只掃描base-package指定下的有@Controller下的Java類,並註冊成beancode

注: context:component-scan節點容許有兩個子節點和。filter標籤的type和表達式說明以下:component

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

在咱們的示例中,將filter的type設置成了正則表達式,regex,注意在正則裏面.表示全部字符,而.才表示真正的.字符。咱們的正則表示以Controller或者Service結束的類。 
咱們也可使用annotaion來限定,如上面註釋掉的所示。這裏咱們指定的include-filter的type是annotation,expression則是註解類的全名。xml

可是由於use-dafault-filter在上面並無指定,默認就爲true,因此當把上面的配置改爲以下所示的時候,就會產生與你指望相悖的結果(注意base-package包值得變化)blog

<context:component-scan base-package="com.sparta.trans">  

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   

</context:component-scan>

 

此時,spring不只掃描了@Controller,還掃描了指定包所在的子包service包下註解@Service的java類

此時指定的include-filter沒有起到做用,只要把use-default-filter設置成false就能夠了。這樣就能夠避免在base-packeage配置多個包名來解決這個問題了。

另外在實際項目開發中咱們能夠發如今base-package指定的包中有的子包是不含有註解的,因此不用掃描,此時能夠指定來進行過濾,說明此包不須要被掃描。因此綜上能夠看出 use-dafault-filters=」false」的狀況下:能夠指定不須要掃描的路徑來排除掃描這些文件,能夠指定須要掃描的路徑來進行掃描。可是因爲use-dafault-filters的值默認爲true,因此這一點在實際使用中仍是須要注意一下的。


@Service告訴spring容器,這是一個Service類,標識持久層Bean組件,默認狀況會自動加載它到spring容器中。 
@Autowried註解告訴spring,這個字段須要自動注入 
@Scope指定此spring bean的scope是單例 
@Repository註解指定此類是一個容器類,是DA層類的實現。標識持久層Bean組件 
@Componet:基本註解,標識一個受Spring管理的Bean組件 
@Controller:標識表現層Bean組件

context.component-scan節點

base-package屬性告訴spring要掃描的包 use-default-filters=」false」表示不要使用默認的過濾器,此處的默認過濾器,會掃描包含Service,Component,Responsitory,Controller註釋修飾類。

相關文章
相關標籤/搜索