【问题标题】:Spring Boot overriding YML profile from Command LineSpring Boot 从命令行覆盖 YML 配置文件
【发布时间】:2015-05-08 14:55:42
【问题描述】:

我不想使用命令行覆盖现有的 YML 文件配置文件,所以我这样做了。

  1. 创建文件夹并添加到类路径中
  2. 在该新文件夹中复制了另一个 application.yml
  3. 运行此命令mvn spring-boot:run -Dspring.profiles.active=unit-test

但它仍然从源代码 application.yml 中获取“默认”活动配置文件。我也尝试创建一个 application.properties 而不是 application.yml 但它仍然没有被拾取?

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SuchApplication implements CommandLineRunner {

    @Autowired
    private DogeService dogeService;

    @Override
    public void run(String... args) {
        System.out.println("AutoConfiguration should have wired up our stuff");
        System.out.println("Let's see if we are doge-worthy...");
        if (dogeService.requiresDogeness()) {
            System.out.println(dogeService.leDoge());
        } else {
            System.out.println("No Doge for us :(");
        }
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SuchApplication.class, args);
    }
}

我的 resources 文件夹下有以下 YML 文件

spring:
  profiles.active: default
---
spring:
  profiles: default

doge:
  wow: 10
  such: so
  very: true

---
spring:
   profiles: unit-test
doge:
  wow: 4
  such: so
  very: true

【问题讨论】:

  • 你的 yml 文件也有 spring.profiles.active 干扰从命令行传入的那个,从 yml 文件中删除那个。

标签: spring spring-boot


【解决方案1】:

我在使用 Spring Boot 时遇到了类似的问题,并在我的配置类中使用此注释解决了它...

@PropertySource("classpath:application.yml")

但是从 Spring 官方文档来看,这个注解看起来没有必要,这也是我第一次尝试时没有添加的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多