当前位置:网站首页>Bean configuration override in boot
Bean configuration override in boot
2022-07-02 15:54:00 【lvhui321】
problem :
The project refers to a third party jar package , Need to be right @Configuration Configure one of the classes bean To cover . The service startup process encountered bean Registered exception 、 Newly added bean The problem of not loading .
give an example :
The third party jar package MybatisPlusConfig class
@Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor innerInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // Pagination interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // Optimism lock interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(this.tenantLineHandler())); interceptor.addInnerInterceptor(new SystemLineInnerInterceptor(this.systemLineHandler())); return interceptor; }}
The current project MybatisPrimaryConfig class
public class MybatisPrimaryConfig { @Bean public MybatisPlusInterceptor innerInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // Pagination interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // Optimism lock interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); interceptor.addInnerInterceptor(new SystemLineInnerInterceptor(this.systemLineHandler())); return interceptor; }}
1. The train of thought is MybatisPrimaryConfig Class method bean name innerInterceptor Cover MybatisPlusConfig Class method bean name innerInterceptor
2. The step is to springboot in ,allowBeanDefinitionOverriding The default is false;spring The default is true. Need to be in application.properties Newly added spring.main.allow-bean-definition-overriding=true.
3. bean Loading order
To configure allowBeanDefinitionOverriding by true after , But there is a problem that the new configuration is not registered [email protected] Properties of scanBasePackages Array , register bean when , It is registered in array order . Write the package name of the referenced package in front of the project package name , Only the configuration class in the project can override the reference package [email protected](scanBasePackages={"com.qm.qfc.common.entity.config","com.qm.qfc.dpf.dpfservice"})
1.spring Medium org.springframework.context.annotation.ComponentScanAnnotationParser Package scan code
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. In the method 1 View in doScan The method is in org.springframework.context.annotation.ClassPathBeanDefinitionScanner class
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified");
Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
// In turn basePackages Class configured in
for (String basePackage : basePackages) {
Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
...
}
return beanDefinitions;
}边栏推荐
- Song of cactus - throwing stones to ask the way (3)
- Wavedec2 in MATLAB, talk about the wavedec2 function [easy to understand]
- Why does the system convert the temp environment variable to a short file name?
- 【5G NR】RRC连接释放
- PHP static members
- Two traversal sequences are known to construct binary trees
- [experience cloud] how to get the metadata of experience cloud in vscode
- 仙人掌之歌——投石问路(3)
- 解决** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the defau
- win10系统升级一段时间后,内存占用过高
猜你喜欢

XPT2046 四线电阻式触摸屏

图数据库|Nebula Graph v3.1.0 性能报告

Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)

《大学“电路分析基础”课程实验合集.实验五》丨线性有源二端网络等效电路的研究

智联招聘的基于 Nebula Graph 的推荐实践分享

Huawei ECS installs mysqlb for mysqld service failed because the control process exited with error code. See “sys

中科大脑知识图谱平台建设及业务实践

又是一年毕业季

How to use percona tool to add fields to MySQL table after interruption

The outline dimension function application of small motherboard
随机推荐
What are the necessary functions of short video app development?
Traversal before, during and after binary tree
/bin/ld: 找不到 -lssl
[salesforce] how to confirm your salesforce version?
idea jar包冲突排查
二叉树前,中,后序遍历
Solve the problem of base64encoder error
纪念成为首个 DAYU200 三方 demo 贡献者
2020.4.12 byte written test questions B DP D monotone stack
【Salesforce】如何确认你的Salesforce版本?
SQL modification statement
The task cannot be submitted after the installation of flick is completed
构建多架构镜像的最佳实践
win10系统升级一段时间后,内存占用过高
Cultural scores of summer college entrance examination
愛可可AI前沿推介(7.2)
Boot 连接 Impala数据库
6090. 极大极小游戏
动态规划入门一,队列的bfs(70.121.279.200)
Fiddler实现手机抓包——入门