springboot 获取yml变量_springboot怎么读取不同yml配置文件

我配置了三个文件夹,dev、prod、test 代表三个不同的环境配置文件,

然后通过最外层的application.yml来加载不同文件夹的配置文件,

其中system.yml的配置为:system:

config: "dev-env"

application-dev.yml为:#启动端口配置

server:

port: 8085

#spring配置

spring:

application:

name: multi-env #应用名

但是通过@Value("${system.config}")

public String config;

报错org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'system.config' in value "${system.config}"

但是可以读取application-dev.yml的配置值@Value("${server.port}")

public String config;

最外层的application.yml用于动态切换环境spring:

profiles:

active: ${profiles.active}

pom.xml配置

org.springframework.boot

spring-boot-maven-plugin

org.apache.maven.plugins

maven-resources-plugin

3.1.0

true

src/main/resources

application.yml

true

src/main/resources/${profiles.active}

**/*.yml

false

src/main/resources

application.yml

true

src/main/resources

**/*.properties

**/*.xml

false

src/main/resources/${profiles.active}

**/*.yml

false

src/main/resources/${profiles.active}/application-${profiles.active}.yml

dev

dev

true

test

test

prod

prod

你可能感兴趣的:(springboot,获取yml变量)