当前位置:   article > 正文

解决SpringBoot启动时无法加载配置文件或环境变量的问题_could not resolve placeholder 'spring.profiles.act

could not resolve placeholder 'spring.profiles.active' in value "${spring.pr


提示:启动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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

二、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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

总结

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/172955
推荐阅读
相关标签
  

闽ICP备14008679号