Springboot使用Maven占位符@替換不生效問題及解決
使用Maven占位符@替換不生效
使用@符號作為占位符,maven變量卻發(fā)現(xiàn)不生效,進入 spring-boot-starter-parent 才發(fā)現(xiàn)有如下配置,也就是說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]相關(guān)文件,我們項目中在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占位符無法替換的報錯排查
開發(fā)環(huán)境
- jdk:1.8
- mybatis:3.4.5
- spring:5.1.9
問題背景和報錯信息
1.Springmvc的項目轉(zhuǎn)成springboot的項目,該項目依賴了一些其他業(yè)務(wù)組的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觸發(fā)了提前初始化,導(dǎo)致PropertyPlaceholderConfigurer 的postProcessBeanFactory方法沒有執(zhí)行,導(dǎo)致占位符沒有被替換。
具體排查過程
思路1的排查過程
刪除依賴的其他項目的xml(如dependency-a.xml, dependency-b.xml),把底層xml里面的PropertyPlaceholderConfigurer顯示的配置在本項目中。
啟動服務(wù),進行debug,在PropertyPlaceholderConfigurer里面打斷點,發(fā)現(xiàn)是可以加載正確的配置信息的。
所以排除這個方向
思路2的排查過程
先逐步的把依賴的其他的xml配置文件一個一個的引入到本項目,直到引入其中一個時,服務(wù)啟動報錯,這樣先定位具體是哪個xml配置文件導(dǎo)致的
把問題xml里面的配置想,copy到本項目,copy過來后,全部注釋掉,從第一個配置項開始,逐一打開注釋,然后啟動服務(wù),進行測試,直到發(fā)現(xiàn)是哪一段配置項導(dǎo)致了目標(biāo)異常
經(jīng)過排查,發(fā)現(xiàn)報錯的觸發(fā)項是一段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,這個地方是罪魁禍?zhǔn)住_@個發(fā)生了報錯,信息如下。
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)過查找,果然本項目內(nèi)部定義了一個和其他jar里面同包名,同類名的 Dao類。
那么把本項目內(nèi)的Dao類改個名字即可,問題解決。
注意:
報錯異常是表象,具體原因還得深究。
這個問題可能不舉報普遍性,主要關(guān)注排查思路。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
String與XML互轉(zhuǎn)以及從XML取節(jié)點值并修改的方法
今天小編就為大家分享一篇String與XML互轉(zhuǎn)以及從XML取節(jié)點值并修改的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07java日期操作工具類(獲取指定日期、日期轉(zhuǎn)換、相隔天數(shù))
這篇文章主要為大家詳細介紹了java日期操作工具類,包括獲取指定日期、日期轉(zhuǎn)換、相隔天數(shù)等操作,感興趣的小伙伴們可以參考一下2016-06-06java中InputStream獲取字節(jié)大小相關(guān)方法詳解
這篇文章主要給大家介紹了關(guān)于java中InputStream獲取字節(jié)大小相關(guān)方法的相關(guān)資料,在Java中要實現(xiàn)讀取文件大小,可以使用InputStream來讀取文件的內(nèi)容,并通過獲取讀取的字節(jié)數(shù)來得到文件的大小,需要的朋友可以參考下2023-11-11SpringBoot中的@RestControllerAdvice注解詳解
這篇文章主要介紹了SpringBoot中的@RestControllerAdvice注解詳解,RestControllerAdvice注解用于創(chuàng)建全局異常處理類,用于捕獲和處理整個應(yīng)用程序中的異常,需要的朋友可以參考下2024-01-01當(dāng)事務(wù)Transactional遇見異步線程出現(xiàn)的坑及解決
這篇文章主要介紹了當(dāng)事務(wù)Transactional遇見異步線程出現(xiàn)的坑及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12