您的位置:首页 >聚焦 >

一看就会用的@ComponentScans示例

2022-04-01 06:06:25    来源:程序员客栈
须弥零一一看就会用的@ComponentScans示例

文章断更四个月了,家里有个小宝宝还真是费精力呀。这个文章也很简单,就是一个示例,看了就会用。为什么要写着呢?大家相比有过这样的一种情况:引入了一个第三方的客户端,需要输入客户端中的Bean,这个时候如果不做配置的话,默认是取不到三方客户端的Bean的。那么有了@ComponentScans这个注解,就可以很方便地获取Bean了。

使用方法

@SpringBootApplication@ComponentScans({    @ComponentScan(basePackages = {"cn.jeremysong"}),  // ①    @ComponentScan(        basePackages = {"org.example"},        excludeFilters = {            @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {  // ②                org.example.TestAService.class            })        }    ),    @ComponentScan(        basePackases = {"com.example"},        includeFileters = {            @ComponnentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {  // ③                com.example.MyAService.class,                com.example.MyBService.class            })        }    ),    @ComponentScan(        basePackages = {"com.custom"},        includeFilters = {            @ComponentScan.Filter(                type = FilterType.CUSTOM,  // ④                value = {SmartFilter.class}            )        }    ),    @ComponentScan(        basePackages = {"com.regex"},        exludeFilters = {            @ComponentScan.Filter(                type = FilterType.REGEX,  // ⑤                pattern = {"^Deleted"}            )        }    ),    @ComponentScan(        basePackages = {"com.annotation"},        includeFilters = {            @ComponentScan.Filter(                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 {  // ⑨}

说明

• ①:当我们使用了@ComponentScans注解后,程序不会和原来一样从Application.java(即程序入口)所在的包为基准进行组件扫描。因此,在使用了@ComponentScans注解之后,需要把自己项目本来的扫描位置给配置上,即@ComponentScan(basePackages = {"cn.jeremysong"})。basePackages表示当前@ComponentScan在指定的包及其子包中扫描搜索符合条件的类。

• ②:excludeFilters表示在这个扫描中排除符合某些规则的类。 FilterType.ASSIGNABLE_TYPE这个枚举表示给定的规则为指定的类,在后面 的参数classes中进行描述。其value是个数组,将指定的class列举出来即可

• ③:includeFileters表示在这个扫描中仅包含符合某些规则的类。 FilterType.ASSIGNABLE_TYPE解释同上

• ④:FilterType.CUSTOM这个枚举表示使用自定义的过滤器,自定义过滤器在本实例中是类SmartFilter.class,具体解释参考第⑦条

• ⑤:FilterType.REGEX这个枚举表示使用正则表达式来匹配类名,寻找符合条件的类。pattern是个数组,可以设置多个正则表达式

• ⑥:FilterType.ANNOTATION这个枚举表示过滤被指定拥有指定注解的类,values中可以配置多个注解。上述实例中用的是自定义的注解Have.class, 代码参考第⑨条

• ⑦:SmartFilter为自定义的 Filter。自定义 Filter 需要继承 org.springframework.core.type.filter.TypeFilter, 重写public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException方法即可。

• ⑧:返回true表示满足条件,返回false则表示不满足条件。可以根据实际需要来自定义。

• ⑨:自定义注解,按照需要编写即可

---- END ----

欢迎关注我的公众号“须弥零一”,原创技术文章第一时间推送。

关键词: 正则表达式 第一时间 就是一个

相关阅读