关于property ‘spring.profiles.active‘ class path resource [application-xx.yml] 的错误的问题

在升级SpringBoot至2.4.3版本后,多环境配置出现错误,报InvalidConfigDataPropertyException,提示'profile.active'属性无效。问题在于2.4版本后配置格式有所改变,需要将`spring.profiles.active`改为`spring.config.activate.on-profile`。修复后,问题得到解决。

最近用springboot写后端服务,之前明明可以运行的多环境配置,突然不奏效了,报如下错误:

org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property 'spring.profiles.active' imported from location 'class path resource [application-test.yml]' is invalid in a profile specific resource [origin: class path resource [application-test.yml] - 3:13]
	at org.springframework.boot.context.config.InvalidConfigDataPropertyException.lambda$throwOrWarn$1(InvalidConfigDataPropertyException.java:124)
	at java.lang.Iterable.forEach(Unknown Source)
	at java.util.Collections$UnmodifiableCollection.forEach(Unknown Source)
	at org.springframework.boot.context.config.InvalidConfigDataPropertyException.throwOrWarn(InvalidConfigDataPropertyException.java:121)
	at org.springframework.boot.context.config.ConfigDataEnvironment.checkForInvalidProperties(ConfigDataEnvironment.java:354)
	at org.springframework.boot.context.config.ConfigDataEnvironment.applyToEnvironment(ConfigDataEnvironment.java:323)
	at org.springframework.boot.context.config.ConfigDataEnvironment.processAndApply(ConfigDataEnvironment.java:236)
	at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:97)
	at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:89)
	at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:100)
	at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:86)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
	at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82)
	at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63)
	at java.util.ArrayList.forEach(Unknown Source)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111)
	at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62)
	at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:362)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)

仔细检查yml文件语法和配置,没有发现错误,查看当前springboot版本如下:

  	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.3</version>
		<relativePath/>
	</parent>

回退到2.4版本之前发现,该错误不会再出现,于是怀疑springBoot版本升级问题导致。

问题解决

2.4之前的的yml文件配置如下:

spring: 
  profiles:
    active: test

2.4版本之后修改为:

spring: 
#  profiles:
#    active: test
  config:
    activate:
      on-profile:
      - test

重新启动,问题解决。

Spring Boot 2.4 及更高版本中,**`spring.profiles`** 这个配置属性已经被 **弃用(Deprecated)**,取而代之的是使用 **`spring.config.activate.on-profile`** 来指定某个配置块所对应的 profile。 --- ## ✅ 原因:为什么弃用 `spring.profiles` Spring Boot 2.4 引入了新的统一配置激活机制(`spring.config.activate.*`),可以更灵活地控制配置的激活方式,比如基于 profile、cloud platform、自定义条件等。 --- ## ✅ 旧写法(已弃用) ```yaml spring: profiles: dev ``` --- ## ✅ 新写法(推荐) ```yaml spring: config: activate: on-profile: dev ``` --- ## ✅ 示例:多环境配置(本地 + 线上) ```yaml # 默认配置(可选) spring: application: name: my-springboot-app --- # 本地开发环境配置 spring: config: activate: on-profile: dev datasource: url: jdbc:mysql://localhost:3306/mydb username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver --- # 线上生产环境配置 spring: config: activate: on-profile: prod datasource: url: jdbc:mysql://prod-db-server:3306/mydb username: prod_user password: prod_password driver-class-name: com.mysql.cj.jdbc.Driver ``` --- ## ✅ 激活某个 profile 的方式不变 你依然可以使用以下方式激活某个 profile: ### 1. 在 `application.yml` 中指定: ```yaml spring: profiles: active: dev ``` > 注意:`spring.profiles.active` 仍然可用,只是 `spring.profiles` 作为配置项被弃用了。 ### 2. 启动参数指定: ```bash java -jar myapp.jar --spring.profiles.active=prod ``` ### 3. IDE 中设置 VM options: ``` -Dspring.profiles.active=dev ``` --- ## ✅ 补充:`@Profile` 注解仍然有效 你仍然可以使用 `@Profile("dev")` 注解来控制某些 Bean 的加载: ```java @Component @Profile("dev") public class DevOnlyComponent { // 只在 dev 环境下注册为 Bean } ``` --- ##
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值