如何使用@component-scan排除不需要的类
文章目錄
- 前因
- 方式一
- 方式二
- 方式三
- 方式四
- 方式五
- 方式六
- 總結(jié)
前因
我們都知道component-scan在springmvc或者springboot中可以掃描包路徑,但是我們?nèi)绾问褂美锩娴膶傩耘懦恍枰獟呙璧念?#xff1f;
方式一
使用spring的xml配置方式實(shí)現(xiàn),這個(gè)是基本功,知道這種方式,那么注解方式就容易理解了
<!-- 定義項(xiàng)目掃描包的路徑,并且排除ApplicationContextConfig和WebSpringMVCServletConfig--> <context:component-scan base-package="com.leo"><context:exclude-filter type="assignable" expression="com.leo.config.ApplicationContextConfig"/><context:exclude-filter type="assignable" expression="com.leo.config.WebSpringMVCServletConfig"/> </context:component-scan>里面的type有以下幾種類型,用法不一一舉例了,大家可以自己嘗試。
| annotation | 過(guò)濾器掃描使用注解標(biāo)注的那些類,并通過(guò)expression指定注解全類名 |
| assignable | 過(guò)濾器掃描派生于expression屬性指定的類 |
| aspectj | 過(guò)濾器掃描與expression屬性所指定的AspectJ表達(dá)式所匹配的那些類 |
| custom | 使用自定義的org.springframework.core.type.TypeFliter實(shí)現(xiàn)類,并通過(guò)expression指定全類名 |
| regex | 過(guò)濾器掃描類的名稱與expression屬性指定正則表達(dá)式所匹配的那些類 |
方式二
這種是通過(guò)@component-scan方式在配置掃描包路徑的時(shí)候直接不包含要排除的包。舉例說(shuō)明一下如下:
項(xiàng)目的包路徑是:com.leo.config、con.leo.controller、con.leo.service
我不需要掃描com.leo.config,那么配置@component-scan的時(shí)候就可以使用下面的配置直接不用掃描com.leo.config
方式三
其實(shí)大部分的情況是我們只有com.leo.config下面的某個(gè)類需要排除,那么可以使用FilterType.ASSIGNABLE_TYPE排除。參考如下配置
@ComponentScan(basePackages = {"com.leo"}, excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {ApplicationContextConfig.class, WebSpringMVCServletConfig.class}))方式四
如果上面的class非常多的話,使用上面的方式就不太合適了,那我們可以使用FilterType.ANNOTATION加自定義注解去實(shí)現(xiàn)。參考如下配置
@ComponentScan(basePackages = {"com.leo"}, excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = ScanIgnore.class))自定義@ScanIgnore實(shí)現(xiàn)
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ScanIgnore { }方式五
也可通過(guò)自定義的過(guò)濾器去實(shí)現(xiàn)。使用FilterType.CUSTOM加上自定義的過(guò)濾器
@ComponentScan(basePackages = {"com.leo"}, excludeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = MyTypeFilter.class))自定義過(guò)濾器MyTypeFilter
public class MyTypeFilter implements TypeFilter{/*** 過(guò)濾當(dāng)前類的信息,如果包含的則不需要掃描** @param metadataReader 讀取當(dāng)前正在掃描的信息* @param metadataReaderFactory 可以獲取到其他任何類的信息* @return* @throws IOException*/@Overridepublic boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {//獲取當(dāng)前類注解的信息AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();//獲取當(dāng)前正在掃描的類信息ClassMetadata classMetadata = metadataReader.getClassMetadata();//獲取當(dāng)前類資源(類的路徑)Resource resource = metadataReader.getResource();String className = classMetadata.getClassName();//包含這個(gè)包路徑的則被攔截if(className.startsWith("com.leo.config")){System.out.println("===============>"+className);return false;}return true;} }方式六
通過(guò)表達(dá)式來(lái)實(shí)現(xiàn)排除,使用FilterType.REGEX,配合pattern來(lái)實(shí)現(xiàn)。參考配合如下:
@ComponentScan(basePackages = {"com.leo"}, excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.leo.config.*"))總結(jié)
以上都是通過(guò)excludeFilters實(shí)現(xiàn)排除不需要類的方式,如果是反向操作,需要添加需要的類,則將上面的關(guān)鍵字替換為includeFilters就好了。如果是配置文件的實(shí)現(xiàn)則
<context:exclude-filter></context:exclude-filter> 替換成 <context:include-filter></context:include-filter>spring的配置文件是基礎(chǔ),后面的無(wú)論是springmvc亦或者是springboot都是在這個(gè)基礎(chǔ)上擴(kuò)展的,所以打好基礎(chǔ)很重要。
總結(jié)
以上是生活随笔為你收集整理的如何使用@component-scan排除不需要的类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java后台架构知识点(待补充)
- 下一篇: SpringMVC学习(五)——零配置实