温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2023-04-04 11:34:50 来源:亿速云 阅读:137 作者:iii 栏目:开发技术

这篇文章主要介绍了Springboot使用Maven占位符@替换不生效问题如何解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Springboot使用Maven占位符@替换不生效问题如何解决文章都会有所收获,下面我们一起来看看吧。

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

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

<resources>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/application*.yml</include>
          <include>**/application*.yaml</include>
          <include>**/application*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <excludes>
          <exclude>**/application*.yml</exclude>
          <exclude>**/application*.yaml</exclude>
          <exclude>**/application*.properties</exclude>
        </excludes>
      </resource>
    </resources>

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

<resources>
    <!-- springboot 默认只替换application*.[yml,yaml,properties]相关文件,我们项目中在bootstrap中需要替换配置中心等相关变量 -->
    <resource>
        <filtering>true</filtering>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
            <include>**/bootstrap*.yml</include>
            <include>**/bootstrap*.yaml</include>
            <include>**/bootstrap*.properties</include>
        </includes>
    </resource>
    <!-- 必须成组否则文件夹下其他资源无法拷贝 -->
    <resource>
        <directory>${basedir}/src/main/resources</directory>
        <excludes>
            <exclude>**/bootstrap*.yml</exclude>
            <exclude>**/bootstrap*.yaml</exclude>
            <exclude>**/bootstrap*.properties</exclude>
        </excludes>
    </resource>
</resources>

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

<bean id="222222" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.xxx.dao"/>
    <property name="sqlSessionFactoryBeanName" value="dependencyCoreSessionFactory"/>
</bean>

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

最终进入到ClassPathBeanDefinitionScanner到doScan方法,

protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
        Assert.notEmpty(basePackages, "At least one base package must be specified");
        Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
        for (String basePackage : basePackages) {
            Set<BeanDefinition> 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类改个名字即可,问题解决。

注意:

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

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

关于“Springboot使用Maven占位符@替换不生效问题如何解决”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Springboot使用Maven占位符@替换不生效问题如何解决”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI