亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何解決SpringBoot啟動時無法加載配置文件或環(huán)境變量問題

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

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

一、錯誤日志

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、在項目的pom.xml文件的<build></build>標(biāo)簽內(nèi)添加如下代碼:

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

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

如下示例:

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

3、<resource>節(jié)點中如果加了<targetPath>子節(jié)點,要注意指定目標(biāo)目錄位置是否正確,否則也可能會導(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>
		<!-- 上面提到的異常,就是因為這個地方指向了錯誤的目錄,導(dǎo)致配置文件掃描不到,最后把這個節(jié)點去掉,就能正常啟動服務(wù)了 -->
		<targetPath>BOOT-INF/classes/</targetPath>
	</resource>
</resources>

總結(jié)

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

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

相關(guān)文章

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

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

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

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

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

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

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

    詳細SpringBoot生命周期接口的使用

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

    深入理解java三種工廠模式

    下面小編就為大家?guī)硪黄钊肜斫鈐ava三種工廠模式。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • Java中i++的一些問題總結(jié)

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

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

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

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

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

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

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

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

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

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

最新評論