您好,登錄后才能下訂單哦!
這篇文章主要介紹了Springboot使用Maven占位符@替換不生效問題如何解決的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Springboot使用Maven占位符@替換不生效問題如何解決文章都會有所收獲,下面我們一起來看看吧。
使用@符號作為占位符,maven變量卻發(fā)現(xiàn)不生效,進(jìn)入 spring-boot-starter-parent 才發(fā)現(xiàn)有如下配置,也就是說springboot除了定義@符號作為占位符之外,其實(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>
所以為了增加對bootstrap.yml文件的支持,增加以下配置,注意資源要include和exclude成組,否則resources文件夾下其他文件不會被拷貝!
<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>
jdk:1.8
mybatis:3.4.5
spring:5.1.9
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后,啟動報(bào)錯,概要信息是說某一個占位符${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)目中。
啟動服務(wù),進(jìn)行debug,在PropertyPlaceholderConfigurer里面打斷點(diǎn),發(fā)現(xiàn)是可以加載正確的配置信息的。
所以排除這個方向
思路2的排查過程
先逐步的把依賴的其他的xml配置文件一個一個的引入到本項(xiàng)目,直到引入其中一個時,服務(wù)啟動報(bào)錯,這樣先定位具體是哪個xml配置文件導(dǎo)致的
把問題xml里面的配置想,copy到本項(xiàng)目,copy過來后,全部注釋掉,從第一個配置項(xiàng)開始,逐一打開注釋,然后啟動服務(wù),進(jìn)行測試,直到發(fā)現(xiàn)是哪一段配置項(xiàng)導(dǎo)致了目標(biāo)異常
經(jīng)過排查,發(fā)現(xiàn)報(bào)錯的觸發(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判斷那里會檢查是否有同名的不兼容的bean,這個地方是罪魁禍?zhǔn)住_@個發(fā)生了報(bào)錯,信息如下。
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)部定義了一個和其他jar里面同包名,同類名的 Dao類。
那么把本項(xiàng)目內(nèi)的Dao類改個名字即可,問題解決。
注意:
報(bào)錯異常是表象,具體原因還得深究。
這個問題可能不舉報(bào)普遍性,主要關(guān)注排查思路。
關(guān)于“Springboot使用Maven占位符@替換不生效問題如何解決”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Springboot使用Maven占位符@替換不生效問題如何解決”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。