這篇文章將為大家詳細(xì)講解有關(guān)Springboot中如何使用@ComponentScan中excludeFilters,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)技術(shù)團(tuán)隊(duì)十多年來致力于為客戶提供網(wǎng)站制作、成都做網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營銷推廣、搜索引擎SEO優(yōu)化等服務(wù)。經(jīng)過多年發(fā)展,公司擁有經(jīng)驗(yàn)豐富的技術(shù)團(tuán)隊(duì),先后服務(wù)、推廣了近1000家網(wǎng)站,包括各類中小企業(yè)、企事單位、高校等機(jī)構(gòu)單位。
首先這邊先給出基礎(chǔ)的service類,該類我想在springboot加載過程中取消自動(dòng)注入。
package com.wyq.test;import org.springframework.stereotype.Service;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */@Service("myBeanService")public class MyService { }
@ComponentScan可以設(shè)置includeFilters和excludeFilters,來自定義過濾器。一般excludeFilters用的比較多。
type = FilterType.ASSIGNABLE_TYPE是根據(jù)類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 可以直接指定這個(gè)類不進(jìn)行自動(dòng)注入。
在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 指定的類要實(shí)現(xiàn)TypeFilter接口,在match方法中可以獲取當(dāng)前掃描到的類的信息,比如注解、類名和類路徑
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"時(shí),match方法返回true,這樣在excludeFilters時(shí),包含"MyService"的類就會(huì)被排除掉
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) { //獲取當(dāng)前類注解的信息 AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); //獲取當(dāng)前類資源(類的路徑) Resource resource = metadataReader.getResource(); ClassMetadata classMetadata = metadataReader.getClassMetadata(); System.out.println("當(dāng)前正在被掃描的類的類名" + classMetadata.getClassName()); if (classMetadata.getClassName().contains("MyService")) { return true; } return false; } }
關(guān)于Springboot中如何使用@ComponentScan中excludeFilters就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁名稱:Springboot中如何使用@ComponentScan中excludeFilters
分享URL:http://chinadenli.net/article4/ihggoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、虛擬主機(jī)、網(wǎng)站排名、商城網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)