交流
商城
MCN
登入
注册
首页
提问
分享
讨论
建议
公告
动态
发表新帖
发表新帖
第7-1 章:@SpringBootApplication的ComponentScan
分享
未结
0
1138
李延
LV6
2021-05-22
悬赏:20积分
# 使用 在@SpringBootApplication的注解上面我们发现: @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) 添加了TypeExcludeFilter与AutoConfigurationExcludeFilter两个用于排除自动扫描的类。我们再下面解析一下都排除了哪些内容 # TypeExcludeFilter ```java @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { if (this.beanFactory instanceof ListableBeanFactory && getClass() == TypeExcludeFilter.class) { for (TypeExcludeFilter delegate : getDelegates()) { if (delegate.match(metadataReader, metadataReaderFactory)) { return true; } } } return false; } private Collection<TypeExcludeFilter> getDelegates() { Collection<TypeExcludeFilter> delegates = this.delegates; if (delegates == null) { delegates = ((ListableBeanFactory) this.beanFactory).getBeansOfType(TypeExcludeFilter.class).values(); this.delegates = delegates; } return delegates; } ``` 我们看到首先通过beanFactory中获取TypeExcludeFilter类型的bean。并一次调用其方法,用于排除扫描的bean。 也就是我们我们可以通过向spring中注入TypeExcludeFilter类型的bean。自定义排除方案。 # AutoConfigurationExcludeFilter ```java @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { return isConfiguration(metadataReader) && isAutoConfiguration(metadataReader); } private boolean isConfiguration(MetadataReader metadataReader) { return metadataReader.getAnnotationMetadata().isAnnotated(Configuration.class.getName()); } private boolean isAutoConfiguration(MetadataReader metadataReader) { return getAutoConfigurations().contains(metadataReader.getClassMetadata().getClassName()); } protected List<String> getAutoConfigurations() { if (this.autoConfigurations == null) { this.autoConfigurations = SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, this.beanClassLoader); } return this.autoConfigurations; } ``` 很明显,是将有EnableAutoConfiguration的类排除掉
回帖
消灭零回复
提交回复
热议榜
java 相关知识分享
8
好的程序员与不好的程序员
6
写给工程师的十条精进原则
5
spring boot以jar包运行配置的logback日志文件没生成
5
一步一步分析SpringBoot启动源码(一)
5
MockMvc测试
5
【吐槽向】是不是有个吐槽的板块比较好玩
4
logstash jdbc同步mysql多表数据到elasticsearch
3
IntelliJ IDEA 优质License Server
3
.gitignore忽略规则
3
SpringBoot启动源码分析
3
一步一步分析SpringBoot启动源码(三)
3
2
一步一步分析SpringBoot启动源码(二)
2
积分不够将无法发表新帖
2
官方产品
Meta-Boot - 基于MCN
MCN - 快速构建SpringBoot应用
微信扫码关注公众号