Springboot使用Maven占位符@替換不生效問題及解決
使用Maven占位符@替換不生效
使用@符號(hào)作為占位符,maven變量卻發(fā)現(xiàn)不生效,進(jìn)入 spring-boot-starter-parent 才發(fā)現(xiàn)有如下配置,也就是說springboot除了定義@符號(hào)作為占位符之外,其實(shí)還限制了其替換范圍!
<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>所以為了增加對(duì)bootstrap.yml文件的支持,增加以下配置,注意資源要include和exclude成組,否則resources文件夾下其他文件不會(huì)被拷貝!
<resources>
? ? <!-- springboot 默認(rèn)只替換application*.[yml,yaml,properties]相關(guān)文件,我們項(xiàng)目中在bootstrap中需要替換配置中心等相關(guān)變量 -->
? ? <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占位符無法替換的報(bào)錯(cuò)排查
開發(fā)環(huán)境
- jdk:1.8
- mybatis:3.4.5
- spring:5.1.9
問題背景和報(bào)錯(cuò)信息
1.Springmvc的項(xiàng)目轉(zhuǎn)成springboot的項(xiàng)目,該項(xiàng)目依賴了一些其他業(yè)務(wù)組的jar,比如dependency-core:0.0.1, dependency-extensions:0.0.1(并非真實(shí)的jar名稱)。該項(xiàng)目的配置基本都是通過xml完成的,在xml里面定義了一些PropertyPlaceholderConfigurer。并且在該項(xiàng)目的xml里面又import了dependency-core和dependency-extensions里面的xml文件,這些xml文件里面也同樣定義了PropertyPlaceholderConfigurer和MapperScanner。
2.項(xiàng)目遷移到springboot后,啟動(dòng)報(bào)錯(cuò),概要信息是說某一個(gè)占位符${dependency.core.db.url}(并非真實(shí)的占位符名稱)找不到。
問題分析思路
PropertyPlaceholderConfigurer里面的配置信息沒有加載到。
有某些bean觸發(fā)了提前初始化,導(dǎo)致PropertyPlaceholderConfigurer 的postProcessBeanFactory方法沒有執(zhí)行,導(dǎo)致占位符沒有被替換。
具體排查過程
思路1的排查過程
刪除依賴的其他項(xiàng)目的xml(如dependency-a.xml, dependency-b.xml),把底層xml里面的PropertyPlaceholderConfigurer顯示的配置在本項(xiàng)目中。
啟動(dòng)服務(wù),進(jìn)行debug,在PropertyPlaceholderConfigurer里面打斷點(diǎn),發(fā)現(xiàn)是可以加載正確的配置信息的。
所以排除這個(gè)方向
思路2的排查過程
先逐步的把依賴的其他的xml配置文件一個(gè)一個(gè)的引入到本項(xiàng)目,直到引入其中一個(gè)時(shí),服務(wù)啟動(dòng)報(bào)錯(cuò),這樣先定位具體是哪個(gè)xml配置文件導(dǎo)致的
把問題xml里面的配置想,copy到本項(xiàng)目,copy過來后,全部注釋掉,從第一個(gè)配置項(xiàng)開始,逐一打開注釋,然后啟動(dòng)服務(wù),進(jìn)行測試,直到發(fā)現(xiàn)是哪一段配置項(xiàng)導(dǎo)致了目標(biāo)異常
經(jīng)過排查,發(fā)現(xiàn)報(bào)錯(cuò)的觸發(fā)項(xiàng)是一段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,逐步跟進(jìn)。
最終進(jìn)入到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判斷那里會(huì)檢查是否有同名的不兼容的bean,這個(gè)地方是罪魁禍?zhǔn)住_@個(gè)發(fā)生了報(bào)錯(cuò),信息如下。
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,經(jīng)過查找,果然本項(xiàng)目內(nèi)部定義了一個(gè)和其他jar里面同包名,同類名的 Dao類。
那么把本項(xiàng)目內(nèi)的Dao類改個(gè)名字即可,問題解決。
注意:
報(bào)錯(cuò)異常是表象,具體原因還得深究。
這個(gè)問題可能不舉報(bào)普遍性,主要關(guān)注排查思路。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)微信App支付服務(wù)端
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微信App支付服務(wù)端,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
java使用list實(shí)現(xiàn)數(shù)據(jù)庫的like功能
這篇文章主要介紹了java使用list實(shí)現(xiàn)數(shù)據(jù)庫的like功能,需要的朋友可以參考下2014-04-04
Spring?Cloud?Alibaba使用Nacos作為注冊(cè)中心和配置中心
這篇文章主要為大家介紹了Spring?Cloud?Alibaba使用Nacos作為注冊(cè)中心和配置中心的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Spring中的NamespaceHandler與BeanDefinitionParser詳解
這篇文章主要介紹了Spring中的NamespaceHandler與BeanDefinitionParser詳解,由Spring啟動(dòng)過程之obtainFreshBeanFactory() ,可以看到NamespaceHandler、BeanDefinitionParser為解析配置文件中的Element起重要作用,那么它本身是如何被加載的呢,需要的朋友可以參考下2023-12-12

