一看就會用的@ComponentScans示例
文章斷更四個月了,家里有個小寶寶還真是費(fèi)精力呀。這個文章也很簡單,就是一個示例,看了就會用。為什么要寫著呢?大家相比有過這樣的一種情況:引入了一個第三方的客戶端,需要輸入客戶端中的Bean,這個時候如果不做配置的話,默認(rèn)是取不到三方客戶端的Bean的。那么有了@ComponentScans這個注解,就可以很方便地獲取Bean了。
使用方法
@SpringBootApplication
@ComponentScans({
????@ComponentScan(basePackages?=?{"cn.jeremysong"}),??//?①
????@ComponentScan(
????????basePackages?=?{"org.example"},
????????excludeFilters?=?{
[email protected](type?=?FilterType.ASSIGNABLE_TYPE,?classes?=?{??//?②
????????????????org.example.TestAService.class
????????????})
????????}
????),
????@ComponentScan(
????????basePackases?=?{"com.example"},
????????includeFileters?=?{
[email protected](type?=?FilterType.ASSIGNABLE_TYPE,?classes?=?{??//?③
????????????????com.example.MyAService.class,
????????????????com.example.MyBService.class
????????????})
????????}
????),
????@ComponentScan(
????????basePackages?=?{"com.custom"},
????????includeFilters?=?{
[email protected](
????????????????type?=?FilterType.CUSTOM,??//?④
????????????????value?=?{SmartFilter.class}
????????????)
????????}
????),
????@ComponentScan(
????????basePackages?=?{"com.regex"},
????????exludeFilters?=?{
[email protected](
????????????????type?=?FilterType.REGEX,??//?⑤
????????????????pattern?=?{"^Deleted"}
????????????)
????????}
????),
????@ComponentScan(
????????basePackages?=?{"com.annotation"},
????????includeFilters?=?{
[email protected](
????????????????type?=?FilterType.ANNOTATION,??//?⑥
????????????????values?=?{
????????????????????com.annotation.Have.class
????????????????}
????????????)
????????}
????)
})
public?class?MyApplication?extends?SpringBootServletInitializer?{
????public?static?void?main(String[]?args)?{
????????SpringApplication.run(MyApplication.class,?args);
????}
}import?org.springframework.core.type.filter.TypeFilter;
public?class?SmartFilter?extends?TypeFilter?{??//?⑦
????@Override
????public?boolean?match(MetadataReader?metadataReader,?MetadataReaderFactory?metadataReaderFactory)?throws?IOException?{
????????String?clzName?=?metadataReader.getClassMetadata().getClassName();
????????return?clzName.startWith("Test");??//?⑧
????}
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public?@interface?Have?{??//?⑨
}說明
??①:當(dāng)我們使用了
@ComponentScans注解后,程序不會和原來一樣從Application.java(即程序入口)所在的包為基準(zhǔn)進(jìn)行組件掃描。因此,在使用了@ComponentScans?注解之后,需要把自己項(xiàng)目本來的掃描位置給配置上,即@ComponentScan(basePackages = {"cn.jeremysong"})。basePackages表示當(dāng)前@ComponentScan?在指定的包及其子包中掃描搜索符合條件的類。??②:
excludeFilters表示在這個掃描中排除符合某些規(guī)則的類。?FilterType.ASSIGNABLE_TYPE這個枚舉表示給定的規(guī)則為指定的類,在后面 的參數(shù)classes中進(jìn)行描述。其value?是個數(shù)組,將指定的class列舉出來即可??③:
includeFileters表示在這個掃描中僅包含符合某些規(guī)則的類。?FilterType.ASSIGNABLE_TYPE解釋同上??④:
FilterType.CUSTOM這個枚舉表示使用自定義的過濾器,自定義過濾器在本實(shí)例中是類SmartFilter.class,具體解釋參考第⑦條??⑤:
FilterType.REGEX這個枚舉表示使用正則表達(dá)式來匹配類名,尋找符合條件的類。pattern是個數(shù)組,可以設(shè)置多個正則表達(dá)式??⑥:
FilterType.ANNOTATION這個枚舉表示過濾被指定擁有指定注解的類,values中可以配置多個注解。上述實(shí)例中用的是自定義的注解Have.class, 代碼參考第⑨條??⑦:
SmartFilter為自定義的 Filter。自定義 Filter 需要繼承?org.springframework.core.type.filter.TypeFilter, 重寫public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException?方法即可。??⑧:返回
true表示滿足條件,返回false則表示不滿足條件??梢愿鶕?jù)實(shí)際需要來自定義。??⑨:自定義注解,按照需要編寫即可
歡迎關(guān)注我的公眾號“須彌零一”,原創(chuàng)技術(shù)文章第一時間推送。
