Java Spring boot 报错 Property 'spring.profiles.active' imported from location is invalid解决方法

本文主要介绍Java中,将Spring cloud 应用程序更新到最新的Spring boot 2.5.0。报错: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]的解决方法。

报错信息:

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$1(InvalidConfigDataPropertyException.java:125)

application.yml:

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

application-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

解决方法

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

参考文档https : //github.com/spring-projects/spring-boot/wiki/Spring-Boot-Config-Data-Migration-Guide

application-dev.yml文件中有内容如下:

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

active: local改成active: dev

或者

可以尝试将application-dev.yml文件名改成application-local.yml,使用local文件。

推荐阅读
cjavapy编程之路首页