从位置 'class path resource [application-dev.yml]' 导入的 属性 'spring.profiles.active' 无效

Property 'spring.profiles.active' imported from location 'class path resource [application-dev.yml]' is invalid

我已将 Spring 云应用程序更新到最新的 Spring 启动版本 2.5.0。

但是在启动过程中我得到这个异常:

11:05:05.038 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property 'spring.profiles.active' imported from location 'class path resource [application-dev.yml]' is invalid in a profile specific resource [origin: class path resource [application-dev.yml] from skyshop-mail-1.0.jar - 42:17]
        at org.springframework.boot.context.config.InvalidConfigDataPropertyException.lambda$throwOrWarn(InvalidConfigDataPropertyException.java:125)

application.yml

spring:
    application:
        name: mail-service
    profiles:
        active: dev

应用-dev.yml文件:

logging:
    file:
        name: ${java.io.tmpdir}/application.log
    level:
        com:
            backend: DEBUG
        org:
            springframework: DEBUG
            springframework.web: DEBUG
jwt:
    expiration: 86400
    secret: test112322
server:
    port: 8020
    servlet:
        context-path: /mail
spring:
    application:
        name: mail-service
    profiles:
        active: local 
    data:
        web:
            pageable:
                one-indexed-parameters: true # Fix pagination starting number to start from 1
        rest:
            basePath: /mail
    jackson:
        default-property-inclusion: non_null
    jmx:
        enabled: false   
    datasource:
        url: jdbc:mariadb://localhost:3306/database
        driverClassName: org.mariadb.jdbc.Driver
        jpa:
            hibernate:
                ddl-auto: update
            properties:
                hibernate:
                    dialect: org.hibernate.dialect.MariaDBDialect
            show-sql: true
        username: root
        password: qwerty
    oauth2:
        resource:
            jwt:
                key-pair:
                    alias: mytestkey
                    store-password: mystorepass
info:
    build:
        version: 1.0
eureka:
    client:
        serviceUrl:
            defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
    instance:
        preferIpAddress: true

你知道我该如何解决这个问题吗?

在您的申请中-dev.yml,您声明:

spring:
    application:
        name: mail-service
    profiles:
        active: local 

2 个解决方案:

  1. 将 application-dev.yml 重命名为 application-local.yml 并使用本地配置文件
  2. 在应用程序中将spring.profiles.active更改为dev-dev.yml

Spring Boot 2.4 改进了 application.properties 和 application.yml 文件的处理方式。

详情请看这里:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Config-Data-Migration-Guide

长话短说:例如,如果您有一个应用程序-local.yml,并且您在其中定义了

spring:
profiles:
    active: local 

然后只需删除 yaml 文件中的此项。

如果文件名是application-dev.yml则无需提及spring.profiles.active属性 (spring启动新版本)

从 2.4 版开始(Spring Boot 2.4):

Profiles can no longer be activated from profile specific documents.

https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4

前进的一种方法可能是使用 spring.profiles.group.*

  1. application-dev.yml 删除:profiles: active: local

  2. 重命名 application-dev.yml -> application-dev123.yml

  3. application.properties中定义组“dev”:spring.profiles.group.dev=local,dev123

名为“dev”的组现在取代了以前名为“dev”的配置文件。

较新的 Spring 引导版本对基于环境的命名约定非常严格。您不能在运行时注入应用程序属性。您需要为每个环境创建 application-{env}.properties,这将根据活动配置文件进行选择。

但是,如果您不习惯进行所有这些更改,您仍然可以使用旧处理器。只需在 运行 Spring 启动时添加以下作为 JVM 参数。如果您使用 Docker.

,请务必将其添加到 Dockerfile Entrypoint 中
-Dspring.config.use-legacy-processing=true

Check documentation here