外化配置
在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁多的XML配置内容,替代它的是在pom.xml中引入模块化的Starter POMs,其中各个模块都有自己的默认配置,所以如果不是特殊应用场景,就只需要在application.properties中完成一些属性配置就能开启各模块的应用。
Spring Boot允许外化(externalize)你的配置,这样你能够在不同的环境下使用相同的代码。你可以使用properties文件,YAML文件,环境变量和命令行参数来外化配置。使用@Value注解,可以直接将属性值注入到你的beans中,并通过Spring的Environment抽象或绑定到结构化对象来访问。
Spring Boot使用一个非常特别的PropertySource次序来允许对值进行合理的覆盖,需要以下面的次序考虑属性:
- 命令行参数
- 来自于java:comp/env的JNDI属性
- Java系统属性(System.getProperties())
- 操作系统环境变量
- 只有在random.*里包含的属性会产生一个RandomValuePropertySource
- 在打包的jar外的应用程序配置文件(application.properties,包含YAML和profile变量)
- 在打包的jar内的应用程序配置文件(application.properties,包含YAML和profile变量)
- 在@Configuration类上的@PropertySource注解
- 默认属性(使用SpringApplication.setDefaultProperties指定)
配置随机值
application.properties文件中添加
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}
random.int*语法是OPEN value (,max) CLOSE,此处OPEN,CLOSE可以是任何字符,并且value,max是整数。如果提供
max,那么value是最小的值,max是最大的值(不包含在内)。
Application属性文件
SpringApplication将从以下位置加载application.properties文件,并把它们添加到Spring Environment中:
- 当前目录下的一个/config子目录
- 当前目录
- 一个classpath下的/config包
- classpath根路径(root)
这个列表是按优先级排序的(列表中位置高的将覆盖位置低的)。
你可以使用YAML(’.yml’)文件替代’.properties’。
如果不喜欢将application.properties作为配置文件名,你可以通过指定spring.config.name环境属性来切换其他的名称。你也可以使用spring.config.location环境属性来引用一个明确的路径(目录位置或文件路径列表以逗号分割)。
如果spring.config.location包含目录(相对于文件),那它们应该以/结尾(在加载前,spring.config.name产生的名称将被追加到后面)。不管spring.config.location是什么值,默认的搜索路径classpath:,classpath:/config,file:,file:config/总会被使用。
以这种方式,你可以在application.properties中为应用设置默认值,然后在运行的时候使用不同的文件覆盖它,同时保留默认
配置。
属性占位符
当application.properties里的值被使用时,它们会被存在的Environment过滤,所以你能够引用先前定义的值(比如,系统属性)。
app.name=MyApp
app.description=${app.name} is a Spring Boot application
YAML代替Properties
YAML是JSON的一个超集,也是一种方便的定义层次配置数据的格式。
Spring框架提供两个便利的类用于加载YAML文档,YamlPropertiesFactoryBean会将YAML作为Properties来加载,
YamlMapFactoryBean会将YAML作为Map来加载。
YAML缺点
YAML文件不能通过@PropertySource注解加载。所以,在这种情况下,如果需要使用@PropertySource注解的方式加载值,那就要使用properties文件。
类型安全的配置属性:
package com.lf.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* Created by LF on 2017/4/11.
*/
@Component
public class UserValue {
@Value("${my.secret}")
private String name;
@Value("${my.number}")
private int age;
@Value("${my.bignumber}")
private long aLong;
@Value("${my.number.less.than.ten}")
private int lessTen;
@Value("${my.number.in.range}")
private int rang;
@Value("${com.if.user.list}")
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getaLong() {
return aLong;
}
public void setaLong(long aLong) {
this.aLong = aLong;
}
public int getLessTen() {
return lessTen;
}
public void setLessTen(int lessTen) {
this.lessTen = lessTen;
}
public int getRang() {
return rang;
}
public void setRang(int rang) {
this.rang = rang;
}
}
使用前缀配置
1.application.properties文件中添加
com.if.user.name=张三
com.if.user.age=12
com.if.user.list=12,13
2.User配置类
package com.lf.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
/**
* Created by LF on 2017/4/11.
*/
@ConfigurationProperties(prefix = "com.if.user")
public class User {
private String name;
private int age;
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3.Application文件
package com.lf;
import com.lf.config.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties({User.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
用新的配置文件
1.新建lf.properties文件
com.if.config.name=张三
com.if.config.age=12
com.if.config.list=12,13
2.需定义如下配置类
package com.lf.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
/**
* Created by LF on 2017/4/11.
*/
@ConfigurationProperties(prefix = "com.if.config")
public class Config {
private String name;
private int age;
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3.Application文件
package com.lf;
import com.lf.config.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource(value = "classpath:lf.properties")
@EnableConfigurationProperties({Config.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}