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)行測(cè)試,直到發(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)目?jī)?nèi)部定義了一個(gè)和其他jar里面同包名,同類名的 Dao類。
那么把本項(xiàng)目?jī)?nèi)的Dao類改個(gè)名字即可,問題解決。
注意:
報(bào)錯(cuò)異常是表象,具體原因還得深究。
這個(gè)問題可能不舉報(bào)普遍性,主要關(guān)注排查思路。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
String與XML互轉(zhuǎn)以及從XML取節(jié)點(diǎn)值并修改的方法
今天小編就為大家分享一篇String與XML互轉(zhuǎn)以及從XML取節(jié)點(diǎn)值并修改的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07java多線程中的生產(chǎn)者和消費(fèi)者隊(duì)列詳解
這篇文章主要介紹了java多線程中的生產(chǎn)者和消費(fèi)者隊(duì)列詳解,隊(duì)列,是一種數(shù)據(jù)結(jié)構(gòu),除了優(yōu)先級(jí)隊(duì)列和LIFO隊(duì)列外,隊(duì)列都是以FIFO(先進(jìn)先出)的方式對(duì)各個(gè)元素進(jìn)行排序的,需要的朋友可以參考下2024-01-01java日期操作工具類(獲取指定日期、日期轉(zhuǎn)換、相隔天數(shù))
這篇文章主要為大家詳細(xì)介紹了java日期操作工具類,包括獲取指定日期、日期轉(zhuǎn)換、相隔天數(shù)等操作,感興趣的小伙伴們可以參考一下2016-06-06java中InputStream獲取字節(jié)大小相關(guān)方法詳解
這篇文章主要給大家介紹了關(guān)于java中InputStream獲取字節(jié)大小相關(guān)方法的相關(guān)資料,在Java中要實(shí)現(xiàn)讀取文件大小,可以使用InputStream來讀取文件的內(nèi)容,并通過獲取讀取的字節(jié)數(shù)來得到文件的大小,需要的朋友可以參考下2023-11-11SpringBoot中的@RestControllerAdvice注解詳解
這篇文章主要介紹了SpringBoot中的@RestControllerAdvice注解詳解,RestControllerAdvice注解用于創(chuàng)建全局異常處理類,用于捕獲和處理整個(gè)應(yīng)用程序中的異常,需要的朋友可以參考下2024-01-01當(dāng)事務(wù)Transactional遇見異步線程出現(xiàn)的坑及解決
這篇文章主要介紹了當(dāng)事務(wù)Transactional遇見異步線程出現(xiàn)的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12