1. 一看就會(huì)用的@ComponentScans示例

        共 3234字,需瀏覽 7分鐘

         ·

        2022-04-01 05:34

        須彌零一一看就會(huì)用的@ComponentScans示例

        文章斷更四個(gè)月了,家里有個(gè)小寶寶還真是費(fèi)精力呀。這個(gè)文章也很簡(jiǎn)單,就是一個(gè)示例,看了就會(huì)用。為什么要寫(xiě)著呢?大家相比有過(guò)這樣的一種情況:引入了一個(gè)第三方的客戶(hù)端,需要輸入客戶(hù)端中的Bean,這個(gè)時(shí)候如果不做配置的話(huà),默認(rèn)是取不到三方客戶(hù)端的Bean的。那么有了@ComponentScans這個(gè)注解,就可以很方便地獲取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?{??//?⑨

        }

        說(shuō)明

        • ??①:當(dāng)我們使用了@ComponentScans注解后,程序不會(huì)和原來(lái)一樣從Application.java(即程序入口)所在的包為基準(zhǔn)進(jìn)行組件掃描。因此,在使用了@ComponentScans?注解之后,需要把自己項(xiàng)目本來(lái)的掃描位置給配置上,即@ComponentScan(basePackages = {"cn.jeremysong"})basePackages表示當(dāng)前@ComponentScan?在指定的包及其子包中掃描搜索符合條件的類(lèi)。

        • ??②:excludeFilters表示在這個(gè)掃描中排除符合某些規(guī)則的類(lèi)。?FilterType.ASSIGNABLE_TYPE這個(gè)枚舉表示給定的規(guī)則為指定的類(lèi),在后面 的參數(shù)classes中進(jìn)行描述。其value?是個(gè)數(shù)組,將指定的class列舉出來(lái)即可

        • ??③:includeFileters表示在這個(gè)掃描中僅包含符合某些規(guī)則的類(lèi)。?FilterType.ASSIGNABLE_TYPE解釋同上

        • ??④:FilterType.CUSTOM這個(gè)枚舉表示使用自定義的過(guò)濾器,自定義過(guò)濾器在本實(shí)例中是類(lèi)SmartFilter.class,具體解釋參考第⑦條

        • ??⑤:FilterType.REGEX這個(gè)枚舉表示使用正則表達(dá)式來(lái)匹配類(lèi)名,尋找符合條件的類(lèi)。pattern是個(gè)數(shù)組,可以設(shè)置多個(gè)正則表達(dá)式

        • ??⑥:FilterType.ANNOTATION這個(gè)枚舉表示過(guò)濾被指定擁有指定注解的類(lèi),values中可以配置多個(gè)注解。上述實(shí)例中用的是自定義的注解Have.class, 代碼參考第⑨條

        • ??⑦:SmartFilter為自定義的 Filter。自定義 Filter 需要繼承?org.springframework.core.type.filter.TypeFilter, 重寫(xiě)public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException?方法即可。

        • ??⑧:返回true表示滿(mǎn)足條件,返回false則表示不滿(mǎn)足條件??梢愿鶕?jù)實(shí)際需要來(lái)自定義。

        • ??⑨:自定義注解,按照需要編寫(xiě)即可


        ---- END ----



        歡迎關(guān)注我的公眾號(hào)“須彌零一”,原創(chuàng)技術(shù)文章第一時(shí)間推送。


        瀏覽 214
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
          
          

            1. 青青草在线免费观看视频 | 美女天堂 | 四虎一区 | 亚洲主播在线 | 黄 片 成 人 片 免费 |