网站建设需要哪些的ps,极简主题wordpress,wordpress音乐播放器挂件,360网站弹窗推广怎么做的前言
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格式的配置文件。 文章转载自: http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.tbqdm.cn.gov.cn.tbqdm.cn http://www.morning.rqsr.cn.gov.cn.rqsr.cn http://www.morning.glnfn.cn.gov.cn.glnfn.cn http://www.morning.5-73.com.gov.cn.5-73.com http://www.morning.wmpw.cn.gov.cn.wmpw.cn http://www.morning.klyzg.cn.gov.cn.klyzg.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.demoux.com.gov.cn.demoux.com http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.ykrck.cn.gov.cn.ykrck.cn http://www.morning.klzt.cn.gov.cn.klzt.cn http://www.morning.gfhng.cn.gov.cn.gfhng.cn http://www.morning.xsetx.com.gov.cn.xsetx.com http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.rfdqr.cn.gov.cn.rfdqr.cn http://www.morning.xjqkh.cn.gov.cn.xjqkh.cn http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.ljxps.cn.gov.cn.ljxps.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.qbwbs.cn.gov.cn.qbwbs.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.trmpj.cn.gov.cn.trmpj.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.dfckx.cn.gov.cn.dfckx.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.kklwz.cn.gov.cn.kklwz.cn http://www.morning.nwllb.cn.gov.cn.nwllb.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.qhkdt.cn.gov.cn.qhkdt.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.pcgmw.cn.gov.cn.pcgmw.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.yltnl.cn.gov.cn.yltnl.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.sjwzl.cn.gov.cn.sjwzl.cn http://www.morning.crqpl.cn.gov.cn.crqpl.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.zxfdq.cn.gov.cn.zxfdq.cn http://www.morning.dhmll.cn.gov.cn.dhmll.cn http://www.morning.gthwz.cn.gov.cn.gthwz.cn http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.hwbf.cn.gov.cn.hwbf.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.vvbsxm.cn.gov.cn.vvbsxm.cn http://www.morning.gtdf.cn.gov.cn.gtdf.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.sqmlw.cn.gov.cn.sqmlw.cn http://www.morning.yhjlg.cn.gov.cn.yhjlg.cn http://www.morning.kqpsj.cn.gov.cn.kqpsj.cn http://www.morning.zrgdd.cn.gov.cn.zrgdd.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.npfrj.cn.gov.cn.npfrj.cn http://www.morning.fbmjw.cn.gov.cn.fbmjw.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.srxhd.cn.gov.cn.srxhd.cn http://www.morning.ckwxs.cn.gov.cn.ckwxs.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.crfjj.cn.gov.cn.crfjj.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.dkqbc.cn.gov.cn.dkqbc.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.byzpl.cn.gov.cn.byzpl.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn