yml/properties
文件配置: name:springbootcommand-line
java -jar xxx.jar --name="springboot"@component public class PropertiesConfiguration{ @Value("${name}") private String name; @PostConstruct public void init(){ System.out.println(name); // springboot } }
//yml or properties 方式 my.secret: ${random.value} my.number: ${random.int} my.bignumber: ${random.long} my.uuid: ${random.uuid} my.number.less.then.ten: ${random.int(10)} //获取10之内的数据 my.number.in.range: ${random.int[1024,1040]} //获取范围数据 //命令行 方式 java -jar xx.jar --my.secret='${random.uuid}'
注
命令行方式中参数java
--
表示经过命令行方式设置 环境属性;SpringApplication.setAddCommandLineProperties(false)
表示关闭命令行方式springboot默认加载的是 application.properties or application.yml 文件; command-line 修改默认加载配置文件
--spring.config.name=myApplication
command-line 指定多个配置文件--spring.config.name='classpath:/myapplication.properties,classpath:/showapplication.properties'
command-line 指定文件加载目录:--spring.config.location='classpath:/,file:./'
加载顺序是从后往前加载;spring
除加载
application.yml
文件之外,还会加载application-default.yml
文件 优先使用-default
配置信息,若是不存在才会从application.yml
中加载;
能够使用spring.profiles.active=dev
指定,则默认加载的指定文件变为:application-dev.yml
;springboot
//赋值到Map中 同理赋值到 Bean中 @ConfigurationProperties(profix='my') public class ConfigurationBean{ private Map<String, String> prod; private List<String> servers; public void setProd(HashMap<String, String> prod) { this.prod = prod; } public void setServers(List<String> servers){ this.servers = servers; } } //配置文件赋值方式 my: dev: url: http://dev.example.com name: Developer setup prod: url: http://prod.example.com name: My CollMap servers: - http://www.baidu.com - http://www.google.com //打印结果: prod: {name=My CollMap, url=http://prod.example.com} servers: [http://www.baidu.com, http://www.google.com] //command-line 赋值方式 java -jar xxx.jar --my.prod.url='http://core.com' --my.prod.name='core name' //打印结果 prod: {name=core name, url=http://core.com} java -jar xxx.jar --my.servers='[http://core.com,http://www.my.com]' servers: [http://core.com, http://www.my.com]
在 yml 单个文件中配置多个环境信息 使用
---
进行分割;
spring.profiles.active= development
表示激活那个配置spring.profiles
表示当前的 配置分类
当前内容必须的再同一个yml文件当中
例如demo:当前启动的端口为 8081app
spring: profiles: active: development server: port: 8080 --- spring: profiles: development server: port: 8081 --- spring: profiles: production server: port: 8082
@Configuration public class EnablePropertiesConfiguration { @ConfigurationProperties(prefix = "environments") @Bean public PropertiesConfigurationOther propertiesConfigurationOther(){ return new PropertiesConfigurationOther(); } } public class PropertiesConfigurationOther { @Value("${name}") private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
-
方式, spring.first-name 能够使用 spring.firstname
大小写
方式,配置文件中,不区分大小写 推荐使用-
方式less
@Getter @Setter public class MyBean { private String name; private String desc; } application.yml environments: myBeans: - name: zhsnagsan desc: shangsan-desc - name: lisi desc: lisi-desc @Component @ConfigurationProperties(prefix = "environments") public class PropertiesConfiguration { private List<MyBean> myBeans; public void setMyBean(List<MyBean> myBeans){ this.myBeans = myBeans; } }
//类上添加 @Validated注解 // 对应的字段上添加 @NotNull //当前若是没有在 yml中配置 myBeans 中的内容 ,致使myBeans为null 则会报错 ...... @Component @ConfigurationProperties(prefix = "environments") @Validated public class PropertiesConfiguration { @NotNull private List<MyBean> myBeans; public void setMyBean(List<MyBean> myBeans){ this.myBeans = myBeans; } }