您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Springboot中如何使用@ComponentScan中excludeFilters,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
首先這邊先給出基礎的service類,該類我想在springboot加載過程中取消自動注入。
package com.wyq.test;import org.springframework.stereotype.Service;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */@Service("myBeanService")public class MyService { }
@ComponentScan可以設置includeFilters和excludeFilters,來自定義過濾器。一般excludeFilters用的比較多。
type = FilterType.ASSIGNABLE_TYPE是根據類class來過濾,后面classes指向類名
package com.wyq.test;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.FilterType;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */@SpringBootApplication@ComponentScan(value = "com.wyq.test", excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {MyService.class})})public class TestApplication { //和上面一樣,省略}
這邊注意下 MyService.class 就是上述的類,通過 FilterType.ASSIGNABLE_TYPE 可以直接指定這個類不進行自動注入。
在com.wyq.test包和子包下,排除有@Service注解的類
package com.wyq.test;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Service;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */@SpringBootApplication@ComponentScan(value = "info.pigg.study.java", excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Service.class})})public class DictApplication { }
type = FilterType.CUSTOM,是自定義過濾,classes 指定的類要實現TypeFilter接口,在match方法中可以獲取當前掃描到的類的信息,比如注解、類名和類路徑
package com.wyq.test;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Service;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */@SpringBootApplication@ComponentScan(value = "info.pigg.study.java", excludeFilters = { @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})})public class DictApplication { }
在類名包含"MyService"時,match方法返回true,這樣在excludeFilters時,包含"MyService"的類就會被排除掉
package com.wyq.test;import org.springframework.core.io.Resource;import org.springframework.core.type.AnnotationMetadata;import org.springframework.core.type.ClassMetadata;import org.springframework.core.type.classreading.MetadataReader;import org.springframework.core.type.classreading.MetadataReaderFactory;import org.springframework.core.type.filter.TypeFilter;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */public class MyTypeFilter implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) { //獲取當前類注解的信息 AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); //獲取當前類資源(類的路徑) Resource resource = metadataReader.getResource(); ClassMetadata classMetadata = metadataReader.getClassMetadata(); System.out.println("當前正在被掃描的類的類名" + classMetadata.getClassName()); if (classMetadata.getClassName().contains("MyService")) { return true; } return false; } }
關于Springboot中如何使用@ComponentScan中excludeFilters就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。