欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何解決SpringBoot啟動(dòng)時(shí)無(wú)法加載配置文件或環(huán)境變量問(wèn)題

 更新時(shí)間:2024年12月12日 08:51:58   作者:林默默  
文章主要介紹了在Spring Boot項(xiàng)目中遇到配置文件加載失敗和資源目錄圖標(biāo)異常的問(wèn)題,并提供了詳細(xì)的解決步驟,解決方法包括在pom.xml文件中添加特定配置,確保資源目錄順序正確,以及注意節(jié)點(diǎn)的正確使用,通過(guò)這些步驟,可以有效解決資源加載問(wèn)題,提高開(kāi)發(fā)效率

提示:?jiǎn)?dòng)springboot服務(wù),發(fā)現(xiàn)加載不了配置文件,resources目錄下的.yml配置文件圖標(biāo)顯示也異常。

一、錯(cuò)誤日志

2022-03-22 10:59:40.815  WARN --- ConfigServletWebServerApplicationContext Line:558 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.lmm.third.ComThirdApplication]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in value "classpath:mongodb-${spring.profiles.active}.properties"
2022-03-22 10:59:40.825 ERROR --- o.s.boot.SpringApplication               Line:826 - Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.lmm.third.ComThirdApplication]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in value "classpath:mongodb-${spring.profiles.active}.properties"
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:188)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:319)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at com.lmm.third.ComThirdApplication.main(ComThirdApplication.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in value "classpath:mongodb-${spring.profiles.active}.properties"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
    at org.springframework.core.env.AbstractEnvironment.resolveRequiredPlaceholders(AbstractEnvironment.java:571)
    at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:460)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:279)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:249)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:303)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:249)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:206)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:174)
    ... 18 common frames omitted

二、resource目錄:

正常的resources資源目錄顯示:


在這里插入圖片描述


非正常resources資源目錄顯示:


在這里插入圖片描述

三、解決

1、在項(xiàng)目的pom.xml文件的<build></build>標(biāo)簽內(nèi)添加如下代碼:

<resources>
	<!-- 該節(jié)點(diǎn)會(huì)掃描src/main/java目錄,若該目錄下有配置文件,則需要添加以下配置,保證文件能夠被掃描和加載到 -->
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <!-- 根據(jù)目錄下的文件配置需要掃描的文件類(lèi)型 -->
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <!-- 該節(jié)點(diǎn)會(huì)掃描src/main/resources,一般資源文件和配置文件會(huì)放在該目錄下 -->
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <!-- 根據(jù)目錄下的文件配置需要掃描的文件類(lèi)型 -->
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

2、另外,如果src/main/resources目錄下還有其他需要掃描指定位置的包,那么配置resource節(jié)點(diǎn)的時(shí)候,要把resource節(jié)點(diǎn)設(shè)置在掃描src/main/resources之前

如下示例:

<resources>
	<!-- 該節(jié)點(diǎn)會(huì)掃描src/main/java目錄,若該目錄下有配置文件,則需要添加以下配置,保證文件能夠被掃描和加載到 -->
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <!-- 根據(jù)目錄下的文件配置需要掃描的文件類(lèi)型 -->
            <include>**/*.properties</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <!-- 掃描指定目錄的配置 -->
    <resource>
        <directory>${project.basedir}/src/main/resources/lib</directory>
        <targetPath>BOOT-INF/lib/</targetPath>
        <includes>
            <include>**/cloud-webapi-sdk7.0.jar</include>
        </includes>
    </resource>
    <!-- 該節(jié)點(diǎn)會(huì)掃描src/main/resources,一般資源文件和配置文件會(huì)放在該目錄下 -->
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <!-- 根據(jù)目錄下的文件配置需要掃描的文件類(lèi)型 -->
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

3、<resource>節(jié)點(diǎn)中如果加了<targetPath>子節(jié)點(diǎn),要注意指定目標(biāo)目錄位置是否正確,否則也可能會(huì)導(dǎo)致掃描文件失敗,

失敗案例如下:

<resources>
	<resource>
		<directory>${project.basedir}/src/main/resources/lib</directory>
		<targetPath>BOOT-INF/lib/</targetPath>
		<includes>
			<include>**/k3cloud-webapi-sdk7.9.jar</include>
		</includes>
	</resource>
	<resource>
		<directory>src/main/resources</directory>
		<!-- 上面提到的異常,就是因?yàn)檫@個(gè)地方指向了錯(cuò)誤的目錄,導(dǎo)致配置文件掃描不到,最后把這個(gè)節(jié)點(diǎn)去掉,就能正常啟動(dòng)服務(wù)了 -->
		<targetPath>BOOT-INF/classes/</targetPath>
	</resource>
</resources>

總結(jié)

踩多一點(diǎn)的坑沒(méi)什么壞處,還能總結(jié)經(jīng)驗(yàn),日后在開(kāi)發(fā)當(dāng)中才能更加細(xì)心,遇到類(lèi)似的異常也能更快排查出來(lái)!

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Selenium處理select標(biāo)簽的下拉框

    Selenium處理select標(biāo)簽的下拉框

    Selenium是一個(gè)開(kāi)源的和便攜式的自動(dòng)化軟件測(cè)試工具,用于測(cè)試Web應(yīng)用程序有能力在不同的瀏覽器和操作系統(tǒng)運(yùn)行。接下來(lái)通過(guò)本文給大家介紹Selenium處理select標(biāo)簽的下拉框,需要的朋友一起學(xué)習(xí)吧
    2016-04-04
  • JVM?運(yùn)行時(shí)數(shù)據(jù)區(qū)與JMM?內(nèi)存模型

    JVM?運(yùn)行時(shí)數(shù)據(jù)區(qū)與JMM?內(nèi)存模型

    這篇文章主要介紹了JVM?運(yùn)行時(shí)數(shù)據(jù)區(qū)與JMM?內(nèi)存模型,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值。需要的朋友可以參考一下
    2022-07-07
  • Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解

    Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解

    這篇文章主要介紹了Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 詳細(xì)SpringBoot生命周期接口的使用

    詳細(xì)SpringBoot生命周期接口的使用

    本文主要介紹了SpringBoot生命周期接口的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 深入理解java三種工廠(chǎng)模式

    深入理解java三種工廠(chǎng)模式

    下面小編就為大家?guī)?lái)一篇深入理解java三種工廠(chǎng)模式。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • Java中i++的一些問(wèn)題總結(jié)

    Java中i++的一些問(wèn)題總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java中i++的一些問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Java Spring 聲明式事務(wù)詳解

    Java Spring 聲明式事務(wù)詳解

    這篇文章主要介紹了spring 聲明式事務(wù)實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Java Swing 多線(xiàn)程加載圖片(保證順序一致)

    Java Swing 多線(xiàn)程加載圖片(保證順序一致)

    這篇文章主要為大家詳細(xì)介紹了Java Swing 多線(xiàn)程加載圖片,保證順序一致,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • mybatis類(lèi)型轉(zhuǎn)換器如何實(shí)現(xiàn)數(shù)據(jù)加解密

    mybatis類(lèi)型轉(zhuǎn)換器如何實(shí)現(xiàn)數(shù)據(jù)加解密

    這篇文章主要介紹了mybatis類(lèi)型轉(zhuǎn)換器如何實(shí)現(xiàn)數(shù)據(jù)加解密,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java從list中取出對(duì)象并獲得其屬性值的方法

    java從list中取出對(duì)象并獲得其屬性值的方法

    這篇文章主要介紹了java從list中取出對(duì)象并獲得其屬性值的方法,大家參考使用
    2013-12-12

最新評(píng)論