目录
2.2、通过@ConfigurationProperties绑定到结构化对象
版本:2.0.8.RELEASE
Spring Boot允许外化(externalize)你的配置,这样你能够在不同的环境下使用相同的代码。你可以使用properties文件,YAML文件,环境变量和命令行参数来外化配置。使用@Value注解,可以直接将属性值注入到你的beans中,或者通过Spring的Environment抽象来访问或通过@ConfigurationProperties绑定到结构化对象。spring boot支持多种外部配置方式,常用以下几种:
- 命令行参数
- 配置文件
更多使用方式请浏览这里
1、命令行参数
通过java -jar app.jar --name="Spring" --server.port=9090
方式来传递参数。
参数用--xxx=xxx
的形式传递。
可以使用的参数可以是我们自己定义的,也可以是Spring Boot中默认的参数。
2、配置文件
springboot会加载下面四个位置的application.properties或者application.yml,优先级从上到下依次降低
1)A /config subdirectory of the current directory当前目录的config子目录
2)The current directory当前目录
3)A classpath /config package类路径的config包
4)The classpath root类根路径
配置文件中参数的调用:
2.1、@Value注解直接注入
配置文件application.yml:
name: ExternalizedConf
代码:
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
@Component
public class MyBean {
@Value("${name}")
private String name;
// ...
}
测试类:
/**
* 测试value注解
*/
public class ValueAnnotationTest extends ApplicationTest {
@Value("${name}")
private String name;
@Test
public void test(){
Assert.assertEquals(name, "ExternalizedConf");
}
}
2.2、通过@ConfigurationProperties绑定到结构化对象
配置文件application.yml:
my:
firstName: yan
list:
- name: my name
description: my description
- name: another name
description: another description
remote-address: 192.168.1.1
servers:
- dev.bar.com
- foo.bar.com
security:
username: user1
password: user1
roles:
- USER
- ADMIN
代码:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 类型安全的配置属性
* 属性加入到spring 的Environment中有两个方法:
* 1,在本类中加component注解
* 2,在启动类上加注解@EnableConfigurationProperties(Config.class)
* 使用 @ConfigurationProperties,还可以生成meta-data文件,以供IDE使用。(摘取网上内容,需验证)
*
* YAML lists的例子:
* my:
* servers:
* - dev.bar.com
* - foo.bar.com
*
* 嵌套的例子
*
*
* 对属性字段名字的匹配,如firstName可以匹配如下4种格式的属性名书写
* my.firstName Standard camel case syntax.
* my.first-name Dashed notation, recommended for use in.properties and .yml files.
* my.first_name Underscore notation, alternative format for use in .properties and .yml files.
* MY_FIRST_NAME Upper case format. Recommended when using a system environment variables.
*
*
* 配置验证:
* @Validated
*/
@Component
@ConfigurationProperties(prefix="my")
public class MyConfig {
private String firstName;
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
private List<String> servers = new ArrayList<String>();
public String getServerStr() {
if(this.servers.size() == 0)
return "";
StringBuilder servers = new StringBuilder();
for(String server : this.servers){
servers.append(server).append(",");
}
servers.setCharAt(servers.length()-1, '\0');
return servers.toString();
}
public void setServers(List<String> servers) {
this.servers = servers;
}
private boolean enabled;
private InetAddress remoteAddress;
private final Security security = new Security();
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public InetAddress getRemoteAddress() {
return remoteAddress;
}
public void setRemoteAddress(InetAddress remoteAddress) {
this.remoteAddress = remoteAddress;
}
public Security getSecurity() {
return security;
}
class Security {
private String username;
private String password;
private List<String> roles = new ArrayList<>(Collections.singleton("USER"));
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<String> getRoles() {
return roles;
}
public void setRoles(List<String> roles) {
this.roles = roles;
}
}
}
测试类:
import com.example.conf.MyConfig;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
/**
* 测试ConfigurationProperty
*/
public class ConfigurationPropertyTest extends ApplicationTest {
private MyConfig myConfig;
@Autowired
public void setMyConfig(MyConfig myConfig){
this.myConfig = myConfig;
}
@Test
public void test(){
Assert.assertEquals(myConfig.getFirstName(), "yan");
}
}
2.3、属性文件中的占位符
app.name=MyApp
app.description=${app.name} is a Spring Boot application
2.4、使用PropertySource
注解指定配置文件位置和名称
可以不使用默认的配置文件名称和路径,通过PropertySource
注解在configuration注解的类上指定。
配置文件:在resource目录下加入文件my.yml
sender_mail: smtp.bitei.cn
代码:在启动类中加入PropertySource注解
@SpringBootApplication
@PropertySource("my.yml")
public class Application {
public static void main( String[] args ) {
SpringApplication app = new SpringApplication(Application.class);
app.run();
}
}
也可以使用ConfigurationProperties注解的location属性指定自定义配置文件