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

 更新时间:2024年12月12日 08:51:58   作者:林默默  
文章主要介绍了在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>

总结

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

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

相关文章

  • Selenium处理select标签的下拉框

    Selenium处理select标签的下拉框

    Selenium是一个开源的和便携式的自动化软件测试工具,用于测试Web应用程序有能力在不同的浏览器和操作系统运行。接下来通过本文给大家介绍Selenium处理select标签的下拉框,需要的朋友一起学习吧
    2016-04-04
  • JVM 运行时数据区与JMM 内存模型

    JVM 运行时数据区与JMM 内存模型

    这篇文章主要介绍了JVM 运行时数据区与JMM 内存模型,文章围绕主题展开详细的内容介绍,具有一定的参考价值。需要的朋友可以参考一下
    2022-07-07
  • Java System.getProperty()-获取系统参数案例详解

    Java System.getProperty()-获取系统参数案例详解

    这篇文章主要介绍了Java System.getProperty()-获取系统参数案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • 详细SpringBoot生命周期接口的使用

    详细SpringBoot生命周期接口的使用

    本文主要介绍了SpringBoot生命周期接口的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • 深入理解java三种工厂模式

    深入理解java三种工厂模式

    下面小编就为大家带来一篇深入理解java三种工厂模式。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • Java中i++的一些问题总结

    Java中i++的一些问题总结

    这篇文章主要给大家介绍了关于Java中i++的一些问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Java Spring 声明式事务详解

    Java Spring 声明式事务详解

    这篇文章主要介绍了spring 声明式事务实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-09-09
  • Java Swing 多线程加载图片(保证顺序一致)

    Java Swing 多线程加载图片(保证顺序一致)

    这篇文章主要为大家详细介绍了Java Swing 多线程加载图片,保证顺序一致,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • mybatis类型转换器如何实现数据加解密

    mybatis类型转换器如何实现数据加解密

    这篇文章主要介绍了mybatis类型转换器如何实现数据加解密,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • java从list中取出对象并获得其属性值的方法

    java从list中取出对象并获得其属性值的方法

    这篇文章主要介绍了java从list中取出对象并获得其属性值的方法,大家参考使用
    2013-12-12

最新评论