配置随机值
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}
访问命令行属性
默认情况下,SpringApplication
将命令行选项参数( 参数以 – 开头,例如 java -jar demo.jar –server.port=9000 )传递给一个 property
,然后将它们添加到 Spring 的Environment
。就像前面所说的,命令行配置的属性总是比其它属性源优先级要高。
如果你不想将命令行的属性添加到Environment
,你可以:
SpringApplication.setAddCommandLineProperties(false)
应用属性文件
SpringApplication
从application.properties
文件中加载属性,然后将它们添加到 Spring 的Environment
中。SpringApplication
会从以下目录寻找属性文件(按从上到下的顺序):
1. 当前目录的/config
子目录
2. 当前目录
4. classpath 下的/config
包
5. classpath 根目录
注意:
你也可以使用yaml文件替代properties文件。
指定配置文件路径
指定配置文件名:
$ java -jar myproject.jar --spring.config.name=myproject
指定配置文件路径:
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
application-{profile}.properties.
通过spring.profiles.active
进行多环境配置。
application-dev.properties--》 开发环境
application-test.properties--》 测试环境
application-prod.propertis --》生产环境
// 加载生产环境配置
java -jar xxx.jar --spring.profiles.active=prod
使用yaml
只要有你的classpath
路径下有SnakeYAML
类库时,SpringApplication
自动支持Yaml 作为 properties的替代。
加载yaml
Spring 框架提供了两种方式加载 YAML 文件:
YamlPropertiesFactoryBean
将 yaml 作为Properties
来加载YamlMapFactoryBean
将 yaml 作为Map
来加载
例如:
environments:
dev:
url: http://dev.example.com
name: Developer Setup
prod:
url: http://another.example.com
name: My Cool App
===>>
environments.dev.url=http://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=http://another.example.com
environments.prod.name=My Cool App
-------------------------------------------------------------
my:
servers:
- dev.example.com
- another.example.com
===>>
my.servers[0]=dev.example.com
my.servers[1]=another.example.com
使用@ConfigurationProperties
绑定属性到类。
@ConfigurationProperties(prefix="my")
public class Config {
private List<String> servers = new ArrayList<String>();
public List<String> getServers() {
return this.servers;
}
}
单个yaml文件配置多环境
server:
address: 192.168.1.100
---
spring:
profiles: dev
server:
address: 127.0.0.1
---
spring:
profiles: prod
server:
address: 192.168.1.120
// 如果spring.profiles.active=dev,server.address=127.0.0.1
// 如果spring.profiles.active=prod,server.address=192.168.1.120
yaml 缺陷
YAML文件不能使用@PropertySource
来加载。
@PropertySource 引入配置文件
在多数情况下,配置信息基本上都是放入application.properties文件中,但在一些场景下,比如某个配置项比较多时,为了分开存放,也可自定义配置文件,如my.properties。由于自定义的文件,系统不会自动加载,这个时候就需要手动引入了。
利用@PropertySource注解既可以引入配置文件,需要引入多个时,可使用@PropertySources设置数组,引入多个文件。
@SpringBootApplication
@PropertySource(value="classpath:my.properties",encoding="utf-8")
public class Chapter3Application {
public static void main(String[] args) {
SpringApplication.run(Chapter3Application.class, args);
}
}
@ConfigurationProperties 和 @EnableConfigurationProerties
config.code=code
config.name=小苗
config.hobby[0]=看电影
config.hobby[1]=旅游
实体类:
//@Component
@EnableConfigurationProperties(value= {Config.class})
@ConfigurationProperties(prefix="config")
@Data
public class Config {
String code;
String name;
List<String> hobby;
}
利用@ConfigurationProperties属性,即可完成配置文件绑定到类。
使用@EnableConfigurationProperties注解将此配置实体注册为bean,或者直接加入@Component使其在启动时被自动扫描到。