SpringBoot入门之分散配置
springboot默认支持两种格式的配置文件:.properties和.yml。其中.properties是属性文件,也是最常用的一种;.yml是yaml格式的文件,yaml是一种简洁的标记语言。例如:在properties文件中定义的spring.data.url,在yaml文件中的定义如下
spring:
data:
url:
从上可以发现yaml层次感更强,具体在项目中选择那种资源文件是没有什么规定的。
spring boot配置
简单案例
首先创建application.properties文件,并定义name=ysl,创建一个名问Name的java类
package com.ysl.entity; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class User { @Value("${name:wdd}")
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
在TestController中调用User对象
package com.ysl.controller; import com.ysl.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; @RestController
public class TestController { @Autowired
private User user; @RequestMapping(value = "/{name}",method = RequestMethod.GET)
@ResponseBody
public String home(@PathVariable("name") String name){
return "hello," + name;
} }
启动该spring boot工程,在浏览器输入http://localhost:8080/ddd,显示出hello,wdd
解析
1.在springboot中默认加载classpath:/,classpath:/config/,file:./,file:./config/ 路径下以application命名的property或yaml文件;
2.参数spring.config.location设置配置文件存放位置
3.参数spring.config.name设置配置文件存放名称
配置文件获取随机数
在springboot中调用Random中的方法可以获取随机数,实例如下:在application.properties中增加age=${random.int},同事修改User对象。会为age生成一个随机值。
@Value("${age}")
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
使用random的时候也可以限制随机数的范围,实例如下:
${random.int()} : 限制生成的数字小于10
${random.int[,]} : 指定范围的数字
在配置文件调用占位符
修改配置文件:
name=ysl
age=${random.int}
remark=hello,my name is ${name},age is ${age}
修改bean:
@Value("$remark")
private String remark;
可以发现将name修改为userName,在配置文件中调用${name}是工程名
去掉@Value
大家可以发现前面在bean中调用配置参数使用的为注解@Value,在spring boot中是可以省去该注解。
配置文件:
userName=liaokailin
age=${random.int[,]}
remark=hello,my name is ${userName},age is ${age}
user.address=china,hangzhou
增加user.address=china,hangzhou,为了调用该参数来使用@ConfigurationProperties。
@Component
@ConfigurationProperties(prefix = "user")
public class User { private @Value("${userName:lkl}") String name;
private @Value("${age}") Integer age;
private @Value("${remark}") String remark;
private String address;
使用@ConfigurationProperties需要指定prefix,同时bean中的属性和配置参数名保持一致。
实体嵌套配置
@Component
@ConfigurationProperties(prefix = "user")
public class User { private @Value("${userName:lkl}") String name;
private @Value("${age}") Integer age;
private @Value("${remark}") String remark;
private String address;
private Address detailAddress;
public class Address {
private String country;
private String province;
private String city;
}
配置文件:
userName=liaokailin
age=${random.int[,]}
remark=hello,my name is ${userName},age is ${age}
user.address=china,hangzhou
user.detailAddress.country=china
user.detailAddress.province=zhejiang
user.detailAddress.city=hangzhou
这种嵌套关系如果通过yaml文件展示出来层次感会更强。
user:
detailAddress:
country:china
province:zhejiang
city:hangzhou
配置集合
一个人可能有多个联系地址,那么地址为集合
@Component
@ConfigurationProperties(prefix = "user")
public class User { private @Value("${userName:lkl}") String name;
private @Value("${age}") Integer age;
private @Value("${remark}") String remark;
private String address;
private Address detailAddress;
private List<Address> allAddress = new ArrayList<Address>();
配置文件:
user.allAddress[].country=china
user.allAddress[].province=zhejiang
user.allAddress[].city=hangzhou user.allAddress[].country=china
user.allAddress[].province=anhui
user.allAddress[].city=anqing
多配置文件
spring boot设置多配置文件很简单,可以在bean上使用注解@Profile("development")即调用application-development.properties|yml文件,也可以调用SpringApplication中的etAdditionalProfiles()方法。
也可以通过启动时指定参数spring.profiles.active。
SpringBoot入门之分散配置的更多相关文章
- 01.springboot入门--启用自动配置注解EnableAutoConfiguration
springboot入门 <parent> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- springboot 入门八-自定义配置信息(编码、拦截器、静态资源等)
若想实际自定义相关配置,只需要继承WebMvcConfigurerAdapter.WebMvcConfigurerAdapter定义些空方法用来重写项目需要用到的WebMvcConfigure实现.具 ...
- springboot 入门二- 读取配置信息一
在上篇入门中简单介绍下springboot启动使用了大量的默认配置,在实际开发过程中,经常需要启动多个服务,那端口如何手动修改呢? 此篇就是简单介绍相关的配置文件信息. Spring Boot允许外部 ...
- SpringBoot入门之简单配置
今天下载了<JavaEE开发的颠覆者SpringBoot实战>这本书,发现Spring还有好多遗漏的部分,算是又恶补了一下,今天主要是学习下SpringBoot的配置. 一.基本配置 1. ...
- SpringBoot入门教程(八)配置logback日志
Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access.logback-c ...
- SpringBoot入门 (三) 日志配置
上一篇博文记录了再springboot项目中读取属性文件中配置的属性,本文学习在springboot项目中记录日志. 日志记录在项目中是很常见的一个功能了,对排查问题有很大帮助,也可以做分类分析及统计 ...
- springboot 入门三- 读取配置信息二(读取属性文件方式)
在上篇文章中简单介绍自带读取方式.springboot提供多种方式来读取 一.@ConfigurationProperties(value="my") 支持更灵活的绑定及元数据的支 ...
- Springboot入门-日志框架配置(转载)
默认情况下,Spring Boot会用Logback来记录日志,并用INFO级别输出到控制台. Logback是log4j框架的作者开发的新一代日志框架,它效率更高.能够适应诸多的运行环境,同时天然支 ...
- SpringBoot入门基础
目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...
随机推荐
- 练习并熟练掌握交互式 SQL 语言
哈工大数据库系统 实验:练习并熟练掌握交互式 SQL 语言 实验目的:基于给定的 OrderDB 数据库, 练习并熟练掌握交互式 SQL 语言实验环境:sql sever 2008 附:Order ...
- doctotext
文档解析库 http://www.it610.com/article/1936051.htm
- windows10 装linux子系统
http://blog.csdn.net/Yuxin_Liu/article/details/52347898 试了一下,下载太慢,就没继续用,可以用实验楼这个网来玩玩linux
- Linux编程规范
1)在使用C语言进行编程时,源文件都必须加---文件头 /******************************************************** *文件名:test.c *创 ...
- 选择排序(直接排序)java语言实现
class demo { public static void main(String[] args) { int[] arr={1,4,2,6,8,9,0,5,3,2,2,4,4,6,7,8}; f ...
- KbmMW 4.5 发布
We are happy to announce the release of kbmMW v. 4.50.00 Professional, Enterprise and CodeGear Editi ...
- 2018.09.23 codeforces 1053A. In Search of an Easy Problem(gcd)
传送门 今天的签到题. 有一个很显然的结论,gcd(n∗m,k)≤2gcd(n*m,k)\le 2gcd(n∗m,k)≤2. 本蒟蒻是用的行列式求三角形面积证明的. 如果满足这个条件,就可以直接构造出 ...
- newton法分形图
方程:z^6-1=0; %f为求解的方程,df是导数,使用的时候用funchandler定义 %res是目标分辨率,iter是循环次数,(xc,yc)是图像的中心,xoom是放大倍数 %参数视自己需求 ...
- asp.net Hessian 服务的注册
Hessian服务端实现了IHttpHandle, 默认情况下是在Web.Config中的handles接点中注册,这样当有 很多实现时比较麻烦 这个时候可以实现IHttpHandleFactory注 ...
- _RecordsetPtr使用方法
_variant_t vUsername,vID,vname; //变量声明 _RecordsetPtr m_pRecordset; //记录集 CString strid; _Connect ...