Spring引导从命令行重写YML配置文件
我不想使用命令行覆盖现有的YML文件配置文件,所以我这样做了Spring引导从命令行重写YML配置文件,spring,spring-boot,Spring,Spring Boot,我不想使用命令行覆盖现有的YML文件配置文件,所以我这样做了 创建文件夹并添加到类路径 在该新文件夹中复制了另一个application.yml 运行此命令mvn spring boot:run-dsspring.profiles.active=单元测试 但它仍然从源代码application.yml中获取“默认”活动概要文件。我还尝试创建了application.properties而不是application.yml,但还是没有得到响应 import org.springframework.
mvn spring boot:run-dsspring.profiles.active=单元测试
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);
}
}
我的资源文件夹下有以下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
我在Spring Boot中遇到了一个类似的问题,并在我的配置类中用这个注释解决了这个问题
@PropertySource("classpath:application.yml")
但是从Spring官方文档来看,这个注释看起来没有必要,这就是为什么我没有在第一次尝试中添加它的原因 您的yml文件也有
spring.profiles.active
,这会干扰从命令行传入的文件,请从yml文件中删除该文件。