当前位置:网站首页>Boot 中bean配置覆盖
Boot 中bean配置覆盖
2022-07-02 12:28:00 【lvhui321】
问题:
项目引用第三方jar包,需要对@Configuration配置类中的某个bean进行覆盖。服务启动过程遇到了bean已被注册异常、新加的bean不加载的问题。
举例:
第三方jar包MybatisPlusConfig 类
@Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor innerInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //分页 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); //乐观锁 interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(this.tenantLineHandler())); interceptor.addInnerInterceptor(new SystemLineInnerInterceptor(this.systemLineHandler())); return interceptor; }}
当前项目MybatisPrimaryConfig 类
public class MybatisPrimaryConfig { @Bean public MybatisPlusInterceptor innerInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //分页 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); //乐观锁 interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); interceptor.addInnerInterceptor(new SystemLineInnerInterceptor(this.systemLineHandler())); return interceptor; }}
1.思路是MybatisPrimaryConfig类的方法bean名字innerInterceptor覆盖MybatisPlusConfig类的方法bean名字innerInterceptor
2.步骤是在springboot中,allowBeanDefinitionOverriding 默认为false;spring默认为true。需要在application.properties中新增spring.main.allow-bean-definition-overriding=true。
3. bean加载顺序
配置allowBeanDefinitionOverriding为true后,却出现新配置不注册的问题。@SpringBootApplication的属性scanBasePackages数组,注册bean时,是按数组顺序注册的。把引用包的包名写在项目包名前面,项目中的配置类才可覆盖掉引用包的bean。@SpringBootApplication(scanBasePackages={"com.qm.qfc.common.entity.config","com.qm.qfc.dpf.dpfservice"})
1.spring中的org.springframework.context.annotation.ComponentScanAnnotationParser包扫描代码
public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final String declaringClass) {
....
Set<String> basePackages = new LinkedHashSet();
String[] basePackagesArray = componentScan.getStringArray("basePackages");
String[] var19 = basePackagesArray;
int var21 = basePackagesArray.length;
int var22;
for(var22 = 0; var22 < var21; ++var22) {
String pkg = var19[var22];
String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg), ",; \t\n");
Collections.addAll(basePackages, tokenized);
}
Class[] var20 = componentScan.getClassArray("basePackageClasses");
var21 = var20.length;
for(var22 = 0; var22 < var21; ++var22) {
Class<?> clazz = var20[var22];
basePackages.add(ClassUtils.getPackageName(clazz));
}
if (basePackages.isEmpty()) {
basePackages.add(ClassUtils.getPackageName(declaringClass));
}
scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {
protected boolean matchClassName(String className) {
return declaringClass.equals(className);
}
});
return scanner.doScan(StringUtils.toStringArray(basePackages));
}
2.在方法1中查看doScan方法是在org.springframework.context.annotation.ClassPathBeanDefinitionScanner类
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified");
Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
//依次对basePackages中配置的类进行注入
for (String basePackage : basePackages) {
Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
...
}
return beanDefinitions;
}边栏推荐
- 【小白聊云】中小企业容器化改造建议
- /Bin/ld: cannot find -lgssapi_ krb5
- Introduction to Dynamic Planning II (5.647.62)
- Pyinstaller's method of packaging pictures attached to exe
- 如何實現十億級離線 CSV 導入 Nebula Graph
- 2279. Maximum number of backpacks filled with stones
- Why does the system convert the temp environment variable to a short file name?
- Golang MD5 encryption and MD5 salt value encryption
- Tree binary search tree
- The sea of stars hidden behind the nebula graph
猜你喜欢

愛可可AI前沿推介(7.2)

使用 percona 工具给 MySQL 表加字段中断后该如何操作

PTA ladder game exercise set l2-001 inter city emergency rescue
![[experience cloud] how to get the metadata of experience cloud in vscode](/img/45/012c2265402ba1b44f4497f468bc61.png)
[experience cloud] how to get the metadata of experience cloud in vscode

Aiko ai Frontier promotion (7.2)

蚂蚁集团大规模图计算系统TuGraph通过国家级评测

Traversal before, during and after binary tree

【Salesforce】如何确认你的Salesforce版本?

Why does the system convert the temp environment variable to a short file name?

Review materials for the special topic of analog electronics with all essence: basic amplification circuit knowledge points
随机推荐
动态规划入门二(5.647.62)
[leetcode] 876 intermediate node of linked list
将点云坐标转换成世界坐标的demo
Pattern matching extraction of specific subgraphs in graphx graph Computing Practice
[leetcode] 577 reverse word III in string
Make p12 certificate [easy to understand]
Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network
[leetcode] 344 reverse string
/Bin/ld: cannot find -lssl
2303. Calculate the total tax payable
蚂蚁集团大规模图计算系统TuGraph通过国家级评测
又是一年毕业季
/Bin/ld: cannot find -llz4
Digital collection system development (program development) - Digital Collection 3D modeling economic model system development source code
二叉树前,中,后序遍历
《大学“电路分析基础”课程实验合集.实验四》丨线性电路特性的研究
【小白聊云】中小企业容器化改造建议
Xpt2046 four wire resistive touch screen
locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db‘: 没有那个文件或目录
树-二叉搜索树