当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Pattern matching extraction of specific subgraphs in graphx graph Computing Practice
愛可可AI前沿推介(7.2)
Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC
HMS core machine learning service helps zaful users to shop conveniently
《大学“电路分析基础”课程实验合集.实验四》丨线性电路特性的研究
Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics
Comment réaliser un graphique Nebula d'importation CSV hors ligne de niveau milliard
Aiko ai Frontier promotion (7.2)
Nebula Graph & 数仓血缘关系数据的存储与读写
Deux séquences ergodiques connues pour construire des arbres binaires
随机推荐
Experiment collection of University "Fundamentals of circuit analysis". Experiment 6 - observation and measurement of typical signals
/bin/ld: 找不到 -llz4
【5G NR】RRC连接释放
[experience cloud] how to get the metadata of experience cloud in vscode
2279. 装满石头的背包的最大数量
6091. Divide the array so that the maximum difference is K
Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network
fastjson List转JSONArray以及JSONArray转List「建议收藏」
蚂蚁集团大规模图计算系统TuGraph通过国家级评测
How to import a billion level offline CSV into Nepal graph
Some problems about pytorch extension
如何实现十亿级离线 CSV 导入 Nebula Graph
Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
Boot 事务使用
/Bin/ld: cannot find -lcrypto
手机app通达信添加自定义公式(分时T+0)为例子讲解
Fiddler realizes mobile packet capturing - getting started
【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
【小白聊云】中小企业容器化改造建议
Wavedec2 in MATLAB, talk about the wavedec2 function [easy to understand]