Spring @ComponentScan注解使用案例詳細(xì)講解
一、簡單介紹
翻開Spring的源碼找到@ComponentScan注解的源碼,發(fā)現(xiàn)注解類上赫然標(biāo)注著Since: 3.1字樣。也就是說,@ComponentScan注解是從Spring的3.1版本開始提供的。在@ComponentScan注解上,標(biāo)注了一個@Repeatable注解,@Repeatable注解的屬性值為ComponentScans.class。再次翻看下@ComponentScans注解的源碼,類上標(biāo)注著Since: 4.3字樣。也就是說,@ComponentScans注解是從Spring4.3版本開始提供的。@ComponentScans注解就相當(dāng)于是@ComponentScan注解的一個數(shù)組,在@ComponentScans注解中可以多次使用@ComponentScan注解來掃描不同的包路徑。
二、注解說明
@ComponentScans注解可以看作是@ComponentScan注解的一個數(shù)組,在@ComponentScans注解中可以多次標(biāo)注@ComponentScan注解。
@ComponentScan注解最核心的功能就是Spring IOC容器在刷新的時候會掃描對應(yīng)包下標(biāo)注了@Component注解、@Configuration注解、@Repository注解、@Service注解和@Controller等等注解的類,生成掃描到的類的Bean定義信息,整體流程與注冊ConfigurationClassPostProcessor類的Bean定義信息的流程基本一致,最終都會將其保存到BeanFactory中的beanDefinitionMap中。
1. @ComponentScans注解源碼
/*** * @since 4.3 * @see ComponentScan */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented public @interface ComponentScans { ComponentScan[] value(); }
可以看到,@ComponentScans注解的源碼還是比較簡單的,在@ComponentScans注解中存在一個ComponentScan[]數(shù)組類型的value屬性,說明@ComponentScans注解的屬性可以存放一個@ComponentScan注解類型的數(shù)組,可以在ComponentScans注解中多次添加@ComponentScan注解。從@ComponentScans注解的源碼還可以看出,@ComponentScans注解從Spring 4.3版本開始提供。
2. @ComponentScan注解源碼
/* * @since 3.1 * @see Configuration */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Repeatable(ComponentScans.class) public @interface ComponentScan { @AliasFor("basePackages") String[] value() default {}; @AliasFor("value") String[] basePackages() default {}; Class<?>[] basePackageClasses() default {}; Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class; ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT; String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN; boolean useDefaultFilters() default true; Filter[] includeFilters() default {}; Filter[] excludeFilters() default {}; boolean lazyInit() default false; @Retention(RetentionPolicy.RUNTIME) @Target({}) @interface Filter { FilterType type() default FilterType.ANNOTATION; @AliasFor("classes") Class<?>[] value() default {}; @AliasFor("value") Class<?>[] classes() default {}; String[] pattern() default {}; } }
可以看到,Spring從3.1版本開始提供@ComponentScan注解,@ComponentScan注解中還有一個內(nèi)部注解@Filter。
@ComponentScan注解中的每個屬性的含義如下所示:
- value:作用同basePackages屬性,String[]數(shù)組類型,指定要掃描的包名。如果指定了要掃描的包名,則Spring會掃描指定的包及其子包下的所有類。
- basePackages:作用同value屬性,String[]數(shù)組類型,指定要掃描的包名。如果指定了要掃描的包名,則Spring會掃描指定的包及其子包下的所有類。
- basePackageClasses:Class<?>[]數(shù)組類型,指定要掃描的類的Class對象。
- nameGenerator:Class<? extends BeanNameGenerator>類型,指定掃描類時,向IOC注入Bean對象時的命名規(guī)則。
- scopeResolver:Class<? extends ScopeMetadataResolver>類型,掃描類時,用于處理并轉(zhuǎn)換符合條件的Bean的作用范圍。
- scopedProxy:ScopedProxyMode類型,指定生成Bean對象時的代理方式,默認(rèn)的代理方法是DEFAULT,也就是不使用代理。關(guān)于ScopedProxyMode的更多詳細(xì)的內(nèi)容,參見后面章節(jié)。
- resourcePattern:String類型,用于指定掃描的文件類型,默認(rèn)是掃描指定包下的**/*.class。
- useDefaultFilters:boolean類型,是否自動檢測@Component @Repository @Service @Controller注解,默認(rèn)是true。
- includeFilters:Filter[]數(shù)組類型,自定義組件掃描過濾規(guī)則,符合過濾規(guī)則的類的Bean定義信息會被注冊到IOC容器中。includeFilters表示只包含對應(yīng)的規(guī)則,當(dāng)使用includeFilters()來指定只包含哪些注解標(biāo)注的類時,需要禁用默認(rèn)的過濾規(guī)則,也就是需要將useDefaultFilters屬性設(shè)置為false。并且,除了符合過濾規(guī)則的類外,Spring內(nèi)置的如下名稱的類的Bean定義信息注冊到IOC容器時不受過濾規(guī)則限制,如下所示:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
- excludeFilters:Filter[]數(shù)組類型,自定義組件掃描過濾規(guī)則,excludeFilters表示排除使用對應(yīng)的規(guī)則,符合過濾規(guī)則的類的Bean定義信息不會被注冊到IOC容器中。
- lazyInit:boolean類型,從Spring4.1版本開始提供,表示Spring掃描組件時是否采用懶加載 ,默認(rèn)false,表示不開啟懶加載。
@Filter注解中的每個屬性的含義如下所示:
- type:FilterType類型,表示過濾規(guī)則的類型。關(guān)于FilterType的更多詳細(xì)的內(nèi)容,參見后面章節(jié)。
- value:Class<?>[]數(shù)組類型,過濾符合規(guī)則的類,作用同classes屬性。
- classes:Class<?>[]數(shù)組類型,過濾符合規(guī)則的類,作用同value屬性。
- pattern:如果FilterType取值為ASPECTJ,則此屬性表示ASPECTJ表達式。
3. ScopedProxyMode枚舉類源碼
ScopedProxyMode枚舉類表示Spring指定生成Bean對象時的代理方式
/* * @since 2.5 * @see ScopeMetadata */ public enum ScopedProxyMode { DEFAULT, NO, INTERFACES, TARGET_CLASS }
ScopedProxyMode類是從Spring 2.5版本開始提供的枚舉類,每個屬性的含義如下所示。
- DEFAULT:默認(rèn)的代理方式,也就是不使用代理,除非在component-scan級別使用了不同的配置。
- NO:不使用代理。
- INTERFACES:基于JDK動態(tài)代理實現(xiàn)接口代理對象。
- TARGET_CLASS:基于CGLib動態(tài)代理創(chuàng)建類代理對象。
4. FilterType枚舉類源碼
FilterType枚舉類表示Spring掃描類時的過濾類型
/* * @since 2.5 */ public enum FilterType { ANNOTATION, ASSIGNABLE_TYPE, ASPECTJ, REGEX, CUSTOM }
FilterType類是Spring2.5版本開始提供的枚舉類,每個屬性的含義如下所示。
- ANNOTATION:按照注解進行過濾。
- ASSIGNABLE_TYPE:按照給定的類型進行過濾。
- ASPECTJ:按照ASPECTJ表達式進行過濾。
- REGEX:按照正則表達式進行過濾。
- CUSTOM:按照自定義規(guī)則進行過濾,使用自定義過濾規(guī)則時,自定義的過濾器需要實現(xiàn)org.springframework.core.type.filter.TypeFilter接口。
在FilterType枚舉類中,ANNOTATION和ASSIGNABLE_TYPE是比較常用的,ASPECTJ和REGEX不太常用,如果FilterType枚舉類中的類型無法滿足日常開發(fā)需求時,可以通過實現(xiàn)org.springframework.core.type.filter.TypeFilter接口來自定義過濾規(guī)則,此時,將@Filter中的type屬性設(shè)置為FilterType.CUSTOM,classes屬性設(shè)置為自定義規(guī)則的類對應(yīng)的Class對象。
三、使用案例
用Spring的注解開發(fā)應(yīng)用程序時,如果需要將標(biāo)注了Spring注解的類注入到IOC容器中,就需要使用@ComponentScan注解來掃描指定包下的類。同時,在Spring4.3版本開始,提供了@ComponentScans注解,在@ComponentScans注解中,支持配置多個@ComponentScan注解來掃描不同的包,配置不同的過濾規(guī)則。
1. 案例描述
使用自定義過濾規(guī)則實現(xiàn)Spring掃描指定包下的類時,使得名稱中含有 componentScanConfig 字符串的類符合過濾規(guī)則。
2. 案例實現(xiàn)
整個案例實現(xiàn)的步驟總體如下所示。
新建自定義過濾規(guī)則類ComponentScanFilter
public class ComponentScanFilter implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { //獲取當(dāng)前正在掃描的類的信息 ClassMetadata classMetadata = metadataReader.getClassMetadata(); //獲取當(dāng)前正在掃描的類名 String className = classMetadata.getClassName(); return className.contains("componentScanConfig"); } }
可以看到,自定義過濾規(guī)則ComponentScanFilter類實現(xiàn)了TypeFilter接口,并覆寫了match()方法,match()方法中的核心邏輯就是:如果類的名稱中含有componentScanConfig字符串,符合過濾規(guī)則,返回true,否則,返回false。
新建配置類ComponentScanConfig
@Configuration @ComponentScan(value = "com.lwk.demo.spring.annocation", includeFilters = { @Filter(type = FilterType.CUSTOM, classes = {ComponentScanFilter.class}) }, useDefaultFilters = false) public class ComponentScanConfig { }
可以看到,在ComponentScanConfig類上標(biāo)注了@Configuration注解,說明ComponentScanConfig類是Spring的配置類。在標(biāo)注的@ComponentScan注解中指定了要掃描的包名,使用只包含的過濾規(guī)則,并采用自定義過濾規(guī)則。
此時,需要注意的是,需要將是否使用默認(rèn)的過濾規(guī)則設(shè)置為false。
新建測試類ComponentScanTest
public class ComponentScanTest { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ComponentScanConfig.class); String[] names = context.getBeanDefinitionNames(); Arrays.stream(names).forEach(System.out::println); } }
可以看到,在ComponentScanTest類中,在AnnotationConfigApplicationContext類的構(gòu)造方法中傳入ComponentScanConfig類的Class對象創(chuàng)建IOC容器,并將其賦值給context局部變量。通過context局部變量的getBeanDefinitionNames()方法獲取所有的Bean定義名稱,隨后遍歷這些Bean定義名稱進行打印。
3. 案例測試
本案例中,在@ComponentScan注解中使用了includeFilters過濾規(guī)則,并且使用的是自定義過濾規(guī)則,符合過濾規(guī)則的是名稱中含有 componentScanConfig 字符串的類。另外,Spring中內(nèi)置的Processor類和Factory類的Bean定義信息注冊到IOC容器時,不受過濾規(guī)則限制。
運行ComponentScanTest類輸出的結(jié)果信息如下所示:
11:14:21.476 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@41975e01
11:14:21.504 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
11:14:21.691 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
11:14:21.694 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
11:14:21.696 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
11:14:21.698 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
11:14:21.711 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'componentScanConfig'
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
componentScanConfig
可以看到,從IOC容器中獲取的Bean的類定義信息的名稱中可以看出,除了名稱中包含componentScanConfig字符串的類符合過濾規(guī)則外,Spring內(nèi)置的Processor類和Factory類不受過濾規(guī)則限制,其類的Bean定義信息都注冊到了IOC容器中。
4. 其他應(yīng)用案例
掃描時排除注解標(biāo)注的類
排除@Controller、@Service和@Repository注解,可以在配置類上通過@ComponentScan注解的excludeFilters()屬性實現(xiàn),如下所示:
@ComponentScan(value = "io.binghe.spring.annotation.chapter02", excludeFilters = { @Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class, Repository.class}) })
掃描時只包含注解標(biāo)注的類
可以使用ComponentScan注解類的includeFilters()屬性來指定Spring在進行包掃描時,只包含哪些注解標(biāo)注的類。如果使用includeFilters()屬性來指定只包含哪些注解標(biāo)注的類時,需要禁用默認(rèn)的過濾規(guī)則。
例如,只包含@Controller注解標(biāo)注的類,可以在配置類上添加@ComponentScan注解,設(shè)置只包含@Controller注解標(biāo)注的類,并禁用默認(rèn)的過濾規(guī)則,如下所示:
@ComponentScan(value = "io.binghe.spring.annotation.chapter02", includeFilters = { @Filter(type = FilterType.ANNOTATION, classes = {Controller.class}) }, useDefaultFilters = false)
重復(fù)注解
在Java8中@ComponentScan注解是一個重復(fù)注解,可以在一個配置類上重復(fù)使用這個注解,如下所示:
@ComponentScan(value = "io.binghe.spring.annotation.chapter02", includeFilters = { @Filter(type = FilterType.ANNOTATION, classes = {Controller.class}) }, useDefaultFilters = false) @ComponentScan(value = "io.binghe.spring.annotation.chapter02", includeFilters = { @Filter(type = FilterType.ANNOTATION, classes = {Service.class}) }, useDefaultFilters = false)
如果使用的是Java8之前的版本,就不能直接在配置類上寫多個@ComponentScan注解了。此時,可以在配置類上使用@ComponentScans注解,如下所示:
@ComponentScans(value = { @ComponentScan(value = "io.binghe.spring.annotation.chapter02", includeFilters = { @Filter(type = FilterType.ANNOTATION, classes = {Controller.class}) }, useDefaultFilters = false), @ComponentScan(value = "io.binghe.spring.annotation.chapter02", includeFilters = { @Filter(type = FilterType.ANNOTATION, classes = {Service.class}) }, useDefaultFilters = false) })
總結(jié):可以使用@ComponentScan注解來指定Spring掃描哪些包,可以使用excludeFilters()指定掃描時排除哪些組件,也可以使用includeFilters()指定掃描時只包含哪些組件。當(dāng)使用includeFilters()指定只包含哪些組件時,需要禁用默認(rèn)的過濾規(guī)則。
掃描時按照指定的類型進行過濾
使用@ComponentScan注解進行包掃描時,按照給定的類型只包含DemoService類(接口)或其子類(實現(xiàn)類或子接口)的組件,如下所示:
@ComponentScan(value = "io.binghe.spring.annotation.chapter02", includeFilters = { @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {DemoService.class}) }, useDefaultFilters = false)
此時,只要是DemoService類型的組件,都會被加載到容器中。也就是說:當(dāng)DemoService是一個Java類時,DemoService類及其子類都會被加載到Spring容器中;當(dāng)DemoService是一個接口時,其子接口或?qū)崿F(xiàn)類都會被加載到Spring容器中。
掃描時按照ASPECTJ表達式進行過濾
使用@ComponentScan注解進行包掃描時,按照ASPECTJ表達式進行過濾,如下所示:
@ComponentScan(value = "io.binghe.spring.annotation.chapter02", includeFilters = { @Filter(type = FilterType.ASPECTJ, classes = {AspectJTypeFilter.class}) }, useDefaultFilters = false)
其中,AspectJTypeFilter類就是自定義的ASPECTJ表達式的過濾器類。
掃描時按照正則表達式進行過濾
使用@ComponentScan注解進行包掃描時,按照正則表達式進行過濾,如下所示:
@ComponentScan(value = "io.binghe.spring.annotation.chapter02", includeFilters = { @Filter(type = FilterType.REGEX, classes = {RegexPatternTypeFilter.class}) }, useDefaultFilters = false)
其中,RegexPatternTypeFilter類就是自定義的正則表達式的過濾器類。
掃描時按照自定義規(guī)則進行過濾
如果實現(xiàn)自定義規(guī)則進行過濾時,自定義規(guī)則的類必須是org.springframework.core.type.filter.TypeFilter接口的實現(xiàn)類。
例如,按照自定義規(guī)則進行過濾,首先,需要創(chuàng)建一個org.springframework.core.type.filter.TypeFilter接口的實現(xiàn)類BingheTypeFilter,如下所示:
public class BingheTypeFilter implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { return false; } }
當(dāng)實現(xiàn)TypeFilter接口時,需要實現(xiàn)TypeFilter接口中的match()方法,match()方法的返回值為boolean類型。當(dāng)返回true時,表示符合過濾規(guī)則,會將類的Bean定義信息注冊到IOC容器中;當(dāng)返回false時,表示不符合過濾規(guī)則,對應(yīng)的類的Bean定義信息不會注冊到IOC容器中。
接下來,使用@ComponentScan注解進行如下配置:
@ComponentScan(value = "io.binghe.spring.annotation.chapter02", includeFilters = { @Filter(type = FilterType.CUSTOM, classes = {BingheTypeFilter.class}) }, useDefaultFilters = false)
到此這篇關(guān)于Spring @ComponentScan注解使用案例詳細(xì)講解的文章就介紹到這了,更多相關(guān)Spring @ComponentScan注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis中xml的動態(tài)sql實現(xiàn)示例
本文主要介紹了Mybatis中xml的動態(tài)sql實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06SpringBoot Logback日志記錄到數(shù)據(jù)庫的實現(xiàn)方法
這篇文章主要介紹了SpringBoot Logback日志記錄到數(shù)據(jù)庫的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11