Profiles can not do
Can no configuration file, direct start on the line, the parameters are default.
Even with the configuration file, the configuration file with the kind of tedious Spring configuration files is not a concept.
SpringBoot configuration file is very simple! Today to talk about.
Configuration File Format
SpringBoot supports two profiles .properties
configuration files and .yml
configuration files, although the official claimed to be the recommended .yml
configuration file, but I actually see or to .properties
profile the majority point.
This is in fact no essential difference between the two formats .properties
files using .
split semantics, .yml
files 缩进
divided sense (a bit like Python ha ha).
So long as we grasp a configuration can be translated into another configuration. Such as configuring port project started.
# .properties文件配置方法
server.port=1007 #启动端口设置为1007
# .yml配置方法
server:
port: 1007 #务必注意冒号后面要有个空格 不然不生效哈
Note loaded at SpringBoot project startup configuration file name by default application.properties
or application.yml
not you recommend old iron while establishing these two files, purely idle nothing good.
TIPS SpringBoot2.x can be used
server.servlet.context-path=/xxx
to access path configuration items, and SpringBoot1.x in configuration method isserver.context-path=/xxx
, I do not know why do not the same, one thing is not forward-compatible annoying!
Custom configuration items
You can customize a number of configuration items, and then be used directly in the project, for example, we usually accustomed to micro-channel public number associated parameters into the configuration file easy to modify, rather than hard-coded into the code, every change had to recompile the program .
# 配置端口
server.port=1007
# 公众号的appid
wxmp.appid=111
# 公众号的secret
wxmp.secret=222
Then injected into the assembly, so that the container can be configured at startup Content injection assembly.
/**
* 微信公众号参数
*/
@Component//注册为组件
public class WxMpParam {
@Value("${wxmp.appid}")//注入wxmp.appid配置项
private String appid;
@Value("${wxmp.secret}")//注入wxmp.secret配置项
private String secret;
}
The specified object binding configuration
Is also effective in the following, attention can not be omitted @Data
/**
* 微信公众号参数
*/
@Component//注册为组件
@EnableConfigurationProperties//启用配置绑定组件功能
@ConfigurationProperties(prefix="wxmp")//指定组件绑定的前缀
@Data//需要开启get/set,以便注入
public class WxMpParam {
private String appid;//自动匹配到wxmp.appid
private String secret;//自动匹配到wxmp.secret
}
Using a random number in the configuration file
Configuration file using the random number is more common scenario, in particular when starting a plurality of clients, it is desirable to start a specified range of port, e.g. 10-20, may be configured as follows:
# 配置端口为1-20间的随机数
server.port=${random.int[10,20]}
So that I can start four consecutive client, start the port are: 12,13,17,19, showing that random, and fluctuations in my specified range.
Custom Profile
Sometimes too many parameters, are placed in a configuration file is too messy, we will want to configure into different files, and each file save different configurations.
Such as the above micro-channel number configured public, we pick out separately, into the wxmp.properties.
# wxmp.properties
# 公众号的appid
wxmp.appid=111
# 公众号的secret
wxmp.secret=222
Let injected into the following code in assembly:
/**
* 微信公众号参数
*/
@Component // 注册为组件
@PropertySource(value = "classpath:wxmp.properties", encoding = "utf-8") // 指定配置文件及编码
public class WxMpParam {
@Value("${wxmp.appid}")
private String appid;
@Value("${wxmp.secret}")
private String secret;
}
Of course, we can also combine syntax bound objects, modified as follows:
/**
* 微信公众号参数
*/
@Component//注册为组件
@EnableConfigurationProperties//启用配置绑定组件功能
@ConfigurationProperties(prefix="wxmp")//指定组件绑定的前缀
@PropertySource(value = "classpath:wxmp.properties", encoding = "utf-8") // 指定配置文件及编码
@Data//需要开启get/set,以便注入
public class WxMpParam {
private String appid;//自动匹配到wxmp.appid
private String secret;//自动匹配到wxmp.secret
}
Reference configuration items
Spring Boot configuration item is a reference value of the other configuration item, this little mention, for example:
# wxmp.properties
# 公众号的appid
wxmp.appid=111
# 公众号的secret,值为111222
wxmp.secret=${wxmp.appid}222
to sum up
Configuring less, in application.properties write directly on the end.
Configure multiple, on the separation of several configuration files.
If you configure a profile in a lot of items, you can also consider the configuration bound object, so rewritable prefix.
SpringBoot provides sufficient flexibility profile uses mechanism that allows us the flexibility to use!