网站建设需要哪些的ps,给wordpress菜单加图标,上海公司网站建设服务,资源分享论坛wordpress前言
Spring Boot 官方提供了两种常用的配置文件格式#xff0c;分别是properties、YML格式。相比于properties来说#xff0c;YML更加年轻#xff0c;层级也是更加分明。
1. properties格式简介
常见的一种配置文件格式#xff0c;Spring中也是用这种格式#xff0c;语…前言
Spring Boot 官方提供了两种常用的配置文件格式分别是properties、YML格式。相比于properties来说YML更加年轻层级也是更加分明。
1. properties格式简介
常见的一种配置文件格式Spring中也是用这种格式语法结构很简单结构为keyvalue。具体如下
userinfo.namemyjszl
userinfo.age25
userinfo.activetrue
userinfo.created-date2018/03/31 16:54:30
userinfo.map.k1v1
userinfo.map.k2v2上述配置文件中对应的实体类如下
Data
ToString
public class UserInfo {private String name;private Integer age;private Boolean active;private MapString,Object map;private Date createdDate;private ListString hobbies;
}2.YML格式简介
以空格的缩进程度来控制层级关系。空格的个数并不重要只要左边空格对齐则视为同一个层级。注意不能用tab代替空格。且大小写敏感。支持字面值对象数组三种数据结构也支持复合结构。 字面值字符串布尔类型数值日期。字符串默认不加引号单引号会转义特殊字符。日期格式支持yyyy/MM/dd HH:mm:ss 对象由键值对组成形如 key:(空格)value 的数据组成。冒号后面的空格是必须要有的每组键值对占用一行且缩进的程度要一致也可以使用行内写法{k1: v1, …kn: vn} 数组由形如 -(空格)value 的数据组成。短横线后面的空格是必须要有的每组数据占用一行且缩进的程度要一致也可以使用行内写法[1,2,…n] 复合结构上面三种数据结构任意组合
如何使用
在src/resources文件夹下创建一个application.yml文件。支持的类型主要有字符串带特殊字符的字符串布尔类型数值集合行内集合行内对象集合对象这几种常用的数据格式。
具体的示例如下
userinfo:age: 25name: myjszlactive: truecreated-date: 2018/03/31 16:54:30map: {k1: v1,k2: v2}hobbies:- one- two- three- 上述配置文件对应的实体类如下
Data
ToString
public class UserInfo {private String name;private Integer age;private Boolean active;private MapString,Object map;private Date createdDate;private ListString hobbies;
}如何从配置文件取值
一切的配置都是为了取值Spring Boot也是提供了几种取值的方式下面一一介绍。
ConfigurationProperties
这个注解用于从配置文件中取值支持复杂的数据类型但是不支持SPEL表达式。 该注解中有一个属性prefix用于指定获配置的前缀毕竟配置文件中的属性很多也有很多重名的必须用一个前缀来区分下。 该注解可以标注在类上也可以标注在方法上这也注定了它有两种获取值的方式。
标注在实体类上 这种方式用于从实体类上取值并且赋值到对应的属性。使用如下 /*** Component 注入到IOC容器中* ConfigurationProperties从配置文件中读取文件*/
Component
ConfigurationProperties(prefix userinfo)
Data
ToString
public class UserInfo {private String name;private Integer age;private Boolean active;private MapString,Object map;private Date createdDate;private ListString hobbies;
}标注在配置类中的方法上 标注在配置类上的方法上同样是从配置文件中取值赋值到返回值的属性中。使用如下 /*** Bean : 将返回的结果注入到IOC容器中* ConfigurationProperties 从配置文件中取值* return*/ConfigurationProperties(prefix userinfo)Beanpublic UserInfo userInfo(){return new UserInfo();}
总结
ConfigurationProperties注解能够很轻松的从配置文件中取值优点如下
支持批量的注入属性只需要指定一个前缀prefix支持复杂的数据类型比如List、Map对属性名匹配的要求较低比如user-nameuser_nameuserNameUSER_NAME都可以取值支持JAVA的JSR303数据校验 注意ConfigurationProperties这个注解仅仅是支持从Spring Boot的默认配置文件中取值比如application.properties、application.yml。
Value
Value这个注解估计很熟悉了Spring中从属性取值的注解支持SPEL表达式不支持复杂的数据类型比如List。使用如下 Value(${userinfo.name})private String UserName;如何从自定义配置文件中取值 Spring Boot在启动的时候会自动加载application.xxx和bootsrap.xxx但是为了区分有时候需要自定义一个配置文件那么如何从自定义的配置文件中取值呢此时就需要配合PropertySource这个注解使用了。 只需要在配置类上标注PropertySource并指定你自定义的配置文件即可完成。如下
SpringBootApplication
PropertySource(value {classpath:custom.properties})
public class DemoApplication {}value属性是一个数组可以指定多个配置文件同时引入。 PropertySource默认加载xxx.properties类型的配置文件不能加载YML格式的配置文件怎么破 如何加载自定义YML格式的配置文件 PropertySource注解有一个属性factory默认值是PropertySourceFactory.class这个就是用来加载properties格式的配置文件我们可以自定义一个用来加载YML格式的配置文件如下
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;import java.io.IOException;
import java.util.Properties;public class YmlConfigFactory extends DefaultPropertySourceFactory {Overridepublic PropertySource? createPropertySource(String name, EncodedResource resource) throws IOException {String sourceName name ! null ? name : resource.getResource().getFilename();if (!resource.getResource().exists()) {return new PropertiesPropertySource(sourceName, new Properties());} else if (sourceName.endsWith(.yml) || sourceName.endsWith(.yaml)) {Properties propertiesFromYaml loadYml(resource);return new PropertiesPropertySource(sourceName, propertiesFromYaml);} else {return super.createPropertySource(name, resource);}}private Properties loadYml(EncodedResource resource) throws IOException {YamlPropertiesFactoryBean factory new YamlPropertiesFactoryBean();factory.setResources(resource.getResource());factory.afterPropertiesSet();return factory.getObject();}}
此时只需要将factory属性指定为YmlConfigFactory即可如下
SpringBootApplication
PropertySource(value {classpath:custom.yml},factory YmlConfigFactory.class)
public class DemoApplication {总结
PropertySource指定加载自定义的配置文件默认只能加载properties格式但是可以指定factory属性来加载YML格式的配置文件。