Springboot使用Maven占位符@替换不生效问题及解决

这篇文章主要介绍了Springboot使用Maven占位符@替换不生效问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

使用Maven占位符@替换不生效

使用@符号作为占位符,maven变量却发现不生效,进入 spring-boot-starter-parent 才发现有如下配置,也就是说springboot除了定义@符号作为占位符之外,其实还限制了其替换范围!

                ${basedir}/src/main/resources         true                    **/application*.yml           **/application*.yaml           **/application*.properties                                ${basedir}/src/main/resources                    **/application*.yml           **/application*.yaml           **/application*.properties                     

所以为了增加对bootstrap.yml文件的支持,增加以下配置,注意资源要include和exclude成组,否则resources文件夹下其他文件不会被拷贝!

                   true         ${basedir}/src/main/resources                      **/bootstrap*.yml             **/bootstrap*.yaml             **/bootstrap*.properties                                 ${basedir}/src/main/resources                      **/bootstrap*.yml             **/bootstrap*.yaml             **/bootstrap*.properties              

spring占位符无法替换的报错排查

开发环境

  • jdk:1.8
  • mybatis:3.4.5
  • spring:5.1.9

问题背景和报错信息

1.Springmvc的项目转成springboot的项目,该项目依赖了一些其他业务组的jar,比如dependency-core:0.0.1, dependency-extensions:0.0.1(并非真实的jar名称)。该项目的配置基本都是通过xml完成的,在xml里面定义了一些PropertyPlaceholderConfigurer。并且在该项目的xml里面又import了dependency-core和dependency-extensions里面的xml文件,这些xml文件里面也同样定义了PropertyPlaceholderConfigurer和MapperScanner。

2.项目迁移到springboot后,启动报错,概要信息是说某一个占位符${dependency.core.db.url}(并非真实的占位符名称)找不到。

问题分析思路

PropertyPlaceholderConfigurer里面的配置信息没有加载到。

有某些bean触发了提前初始化,导致PropertyPlaceholderConfigurer 的postProcessBeanFactory方法没有执行,导致占位符没有被替换。

具体排查过程

思路1的排查过程

删除依赖的其他项目的xml(如dependency-a.xml, dependency-b.xml),把底层xml里面的PropertyPlaceholderConfigurer显示的配置在本项目中。

启动服务,进行debug,在PropertyPlaceholderConfigurer里面打断点,发现是可以加载正确的配置信息的。

所以排除这个方向

思路2的排查过程

先逐步的把依赖的其他的xml配置文件一个一个的引入到本项目,直到引入其中一个时,服务启动报错,这样先定位具体是哪个xml配置文件导致的

把问题xml里面的配置想,copy到本项目,copy过来后,全部注释掉,从第一个配置项开始,逐一打开注释,然后启动服务,进行测试,直到发现是哪一段配置项导致了目标异常

经过排查,发现报错的触发项是一段mapper scanner

          

然后在MapperScannerConfigurer的postProcessBeanDefinitionRegistry方法中debug,逐步跟进。

最终进入到ClassPathBeanDefinitionScanner到doScan方法,

protected Set doScan(String... basePackages) {         Assert.notEmpty(basePackages, "At least one base package must be specified");         Set beanDefinitions = new LinkedHashSet<>();         for (String basePackage : basePackages) {             Set candidates = findCandidateComponents(basePackage);             for (BeanDefinition candidate : candidates) {                 ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);                 candidate.setScope(scopeMetadata.getScopeName());                 String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);                 if (candidate instanceof AbstractBeanDefinition) {                     postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);                 }                 if (candidate instanceof AnnotatedBeanDefinition) {                     AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);                 }                 if (checkCandidate(beanName, candidate)) {                     BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);                     definitionHolder =                             AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);                     beanDefinitions.add(definitionHolder);                     registerBeanDefinition(definitionHolder, this.registry);                 }             }         }         return beanDefinitions;     }

最后的if判断那里会检查是否有同名的不兼容的bean,这个地方是罪魁祸首。这个发生了报错,信息如下。

Annotation-specified bean name 'xxxDao' for bean class [com.xxx.XXXDao] 
conflicts with existing, 
non-compatible bean definition of same name and class [org.mybatis.spring.mapper.MapperScannerConfigurer]

这就说明有同名的Bean,经过查找,果然本项目内部定义了一个和其他jar里面同包名,同类名的 Dao类。

那么把本项目内的Dao类改个名字即可,问题解决。

注意:

报错异常是表象,具体原因还得深究。

这个问题可能不举报普遍性,主要关注排查思路。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持0133技术站。

以上就是Springboot使用Maven占位符@替换不生效问题及解决的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java