java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot启动时无法加载配置文件或环境变量

如何解决SpringBoot启动时无法加载配置文件或环境变量问题

作者:林默默

文章主要介绍了在Spring Boot项目中遇到配置文件加载失败和资源目录图标异常的问题,并提供了详细的解决步骤,解决方法包括在pom.xml文件中添加特定配置,确保资源目录顺序正确,以及注意节点的正确使用,通过这些步骤,可以有效解决资源加载问题,提高开发效率

提示:启动springboot服务,发现加载不了配置文件,resources目录下的.yml配置文件图标显示也异常。

一、错误日志

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>标签内添加如下代码:

<resources>
	<!-- 该节点会扫描src/main/java目录,若该目录下有配置文件,则需要添加以下配置,保证文件能够被扫描和加载到 -->
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <!-- 根据目录下的文件配置需要扫描的文件类型 -->
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <!-- 该节点会扫描src/main/resources,一般资源文件和配置文件会放在该目录下 -->
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <!-- 根据目录下的文件配置需要扫描的文件类型 -->
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

2、另外,如果src/main/resources目录下还有其他需要扫描指定位置的包,那么配置resource节点的时候,要把resource节点设置在扫描src/main/resources之前

如下示例:

<resources>
	<!-- 该节点会扫描src/main/java目录,若该目录下有配置文件,则需要添加以下配置,保证文件能够被扫描和加载到 -->
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <!-- 根据目录下的文件配置需要扫描的文件类型 -->
            <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>
    <!-- 该节点会扫描src/main/resources,一般资源文件和配置文件会放在该目录下 -->
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <!-- 根据目录下的文件配置需要扫描的文件类型 -->
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

3、<resource>节点中如果加了<targetPath>子节点,要注意指定目标目录位置是否正确,否则也可能会导致扫描文件失败,

失败案例如下:

<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>
		<!-- 上面提到的异常,就是因为这个地方指向了错误的目录,导致配置文件扫描不到,最后把这个节点去掉,就能正常启动服务了 -->
		<targetPath>BOOT-INF/classes/</targetPath>
	</resource>
</resources>

总结

踩多一点的坑没什么坏处,还能总结经验,日后在开发当中才能更加细心,遇到类似的异常也能更快排查出来!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文