Spring Properties整理
spring 配置及配置文件,是学习spring的基础知识,那么,这篇文档我们先了解下spring 的配置写法,以及如何加载配置的。
解析 application.yaml
YAML语法
YAML基本语法:
- 以键值对的方式表示属性。(空格必须有)。
- 使用缩进表示层级关系
- 缩进时不允许使用Tab键,只允许使用空格。
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可。
- 属性和值是大小写敏感的。
【:后必须跟一个空格】,两种方式推荐使用yml方式配置项目。
YAML支持的三种数据结构。
- 字面量:普通的值。(数字,字符串,布尔)
-字符串不加引号
-双引号是为了有转义字符
- 单引号不转义
- 对象:键值对的集合,key 冒号,对应value。(Map)
- 数组:一组按次序排列的值, 一般的中间逗号隔开。(List,Set)
YMAL写的注意事项
字面量:
以key: value来表示,value前必须带一个空格。
port: 8080
对象、Map
第一种写法
UsersInfo:
name: DerikLi
email: sodingli@hotmail.com
第二种写法
大括号的方式,还有逗号
UsersInfo: {
name: DerikLi,
email: sodingli@hotmail.com
}
数组
数组用 “-”表示。
UsersInfo:
- name
- name
- age
当然,还有一种,冒号不要丢失
UsersInfo: [name,name,age]
配置映射
Spring
的配置通过配置文件进行映射。分为两种方式,一种是 @ConfigurationProperties
读取属性,一种是@Value
这种读取单个的值。
通过 ConfigurationProperties 整块映射属性值
类文件为
@ConfigurationProperties("monapi")
@Data
public class MonitorApiproperties {
private String uris;
private int interval;
private String hostname;
private List<String> xml = new ArrayList<>(); //指定获取多个配置
}
monapi:
uris: http://monitor.com:8080
interval: 10
hostname: monapi-test-vm01
xml: classpath:esmapper/*/*.xml,,classpath:etcdmapper/*.xml
这是一种通过路径monapi
映射方式。
这里如果
支持对 通过类加载的方式,对配置的值进行映射。
加载配置属性
映射完成后,我们还得让Spring 能扫描的到。 那么又可以用到两种方式。
让 Spring 知道我们的 @ConfigurationProperties 类存在,以便将其加载到应用程序上下文中。
第一种,就是让自定义的类,注册成为一个启动类的配置,然后的加载方式。@EnableConfigurationProperties({MonitorApiproperties.class})
可以在启动类了,就是主入口类,加载配置
@SpringBootApplication
@EnableConfigurationProperties({MonitorApiproperties.class})//here
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
第二种,可以直接在 ConfigurationProperties @ComponentScan 注解参数。
@Component //加上组件类
@ConfigurationProperties("monapi")
public class MonitorApiproperties {
private String uris;
private int interval;
private String hostname;
private List<String> xml = new ArrayList<>(); //指定获取多个配置
public List<String> getUris() {
return uris;
}
public void setUris(List<String> uris) {
this.uris = uris;
}
public int getInterval() {
return interval;
}
public void setInterval(int interval) {
this.interval = interval;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
还可以通过bean 生成配置。 也可以直接用了,当然如果是bean 加载的话,就不用在
MonitorApiproperties
注释 @Component
了。
@Configuration
public class MonPropertiesConfigration {
@Bean
public MonitorApiproperties monitorApiproperties(){
return new MonitorApiproperties();
}
}
获取参数值,用于构建客户端
调用,一般都是在 Configuration
去取参数,就可以直接用了。
@Configuration
public class MonPropertiesConfigration {
@Bean
public Client client(MonitorApiproperties configraton) {
try {
PreBuiltTransportClient client = new PreBuiltTransportClient(configraton.getUris()[0]);
return client;
} catch (Exception e) {
throw new RuntimeException("build transport client error", e);
}
}
}
通过 value 映射属性值
这就比较简单了,直接获取值就行了,只能一个值一个值的获取。
@Component
public class PrometheusRepostitoryImpl implements PrometheusRepostitory {
@Value("${spring.prometheus.host}")
private String host;
@Value("${spring.prometheus.port}")
private String port;
}
Spring 配置文件加载顺序
配置文件加载位置和顺序,springboot启动会扫描一下位置的配置文件作为springboot的默认配置文件。
- 项目路径下的config文件夹
- 项目路径下
- classpath路径下config文件夹
- classpath路径下
SpringBoot也可以从以下位置加载配置;优先级从高到低,高优先级的配置覆盖低优先级的配置,所有配置形成互补配置。
- 命令行参数
- 来自java:comp/env的JNDI属性
- Java系统属性(System.getProperties())
- 操作系统环境变量
- RandomValuePropertySource配置的random.*属性值
- jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
- jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
- jar包外部的application.properties或application.yml(不带spring.profile)配置文件
- jar包内部的application.properties或application.yml(不带spring.profile)配置文件
- @Configuration注解类上的@PropertySource
- 通过SpringApplication.setDefaultProperties指定的默认属性
多配置文件读取规则
有时候需要配置多个配置文件,一个主配置文件,application.yml和开发配置文件,和生产环境配置。
application.yml
application-dev.yml
application-prod.yml
我们通过在主配置文件用
spring.profile.active = dev(prod) #来选择需要加装的配置文件。
配置命名
而在 application-dev.yml 和 application-prod.yml ,为了能找到 dev(prod) 的标签,我们通过 ,配置名称方式,来读取。
spring:
profiles: dev #给文档命名为dev
可以使用spring.profile.active来进行切换。
也可以通过命令行指定配置文件
Copyjava –jar springboot02-0.0.1-SHAPSHOT.jar –spring.profiles.active=dev