解决Property ‘spring.profiles.active‘ imported from location ‘application-xxx.yml‘ is invalid in...的问题
  TEZNKK3IfmPf 2023年11月12日 19 0

1. 问题

今天启动spring boot项目时,突然出现如下图所示问题:

解决Property ‘spring.profiles.active‘ imported from location ‘application-xxx.yml‘ is invalid in...的问题

Property 'spring.profiles.active' imported from location 'class path resource [application-local.yml]' is invalid in a profile specific resource [origin: class path resource [application-local.yml] - 34:13]

2. 分析问题

  1. 首先查看application-local.yml配置文件是否有问题
# 端口号配置
server:
  port: 8081

# spring 配置
spring:
  profiles:
    active: local
  application:
    name: superJsonManager
  session:
    store-type: jdbc
    jdbc:
      initialize-schema: always
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    password: 123456
    username: root
    url: jdbc:mysql://localhost:3306/superjson?useUnicode=true&characterEncoding=utf8&useSSL=false

# mybatis配置
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  check-config-location: true
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.superjson.superjsonmanager.mapper

没有发现application-local.yml文件存在问题

  1. 查看所配置的环境是否存在问题

解决Property ‘spring.profiles.active‘ imported from location ‘application-xxx.yml‘ is invalid in...的问题

没有发现配置环境存在问题。

application-local.yml文件没有问题,配置环境没有问题,那么,哪里出现了问题呢?

只可能是spring boot版本的问题,因为spring boot的2.5版本前后的语法是不一样的。

3. 解决问题

既然知道了是spring boot版本的问题,那么我们就可以进行如下修改。

  1. spring boot版本降低到2.5以下,如下所示:
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.4.3</version>
</parent>

但这不建议这样做,因为降低版本可能会导致以下问题:

  1. 导致程序无法运行,因为先前的代码存在其它版本的依赖。

  2. 可能无法使用高版本的依赖。

  3. 可能无法 引入新的依赖,因为新的依赖可能不支持2.4版本下的spring boot

因而,不建议降低版本。

  1. 建议修改yaml语法,符合高版本的spring boot

我的spring boot依赖版本是2.7.1,如下所示:

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.7.1</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

我的依赖版本大于2.4,需要采用如下方式:

# spring 配置
spring:
# profiles:
# active: lcoal
  config:
    activate:
      on-profile:
        - lcoal

如此配置,即可正常启动,如下图所示:

解决Property ‘spring.profiles.active‘ imported from location ‘application-xxx.yml‘ is invalid in...的问题

如果是2.4以前的版本,可以这样配置:

# spring 配置
spring:
   profiles:
     active: lcoal
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月17日   46   0   0 JSpspring
TEZNKK3IfmPf