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入门之分散配置的更多相关文章

  1. 01.springboot入门--启用自动配置注解EnableAutoConfiguration

    springboot入门 <parent> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  2. springboot 入门八-自定义配置信息(编码、拦截器、静态资源等)

    若想实际自定义相关配置,只需要继承WebMvcConfigurerAdapter.WebMvcConfigurerAdapter定义些空方法用来重写项目需要用到的WebMvcConfigure实现.具 ...

  3. springboot 入门二- 读取配置信息一

    在上篇入门中简单介绍下springboot启动使用了大量的默认配置,在实际开发过程中,经常需要启动多个服务,那端口如何手动修改呢? 此篇就是简单介绍相关的配置文件信息. Spring Boot允许外部 ...

  4. SpringBoot入门之简单配置

    今天下载了<JavaEE开发的颠覆者SpringBoot实战>这本书,发现Spring还有好多遗漏的部分,算是又恶补了一下,今天主要是学习下SpringBoot的配置. 一.基本配置 1. ...

  5. SpringBoot入门教程(八)配置logback日志

    Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access.logback-c ...

  6. SpringBoot入门 (三) 日志配置

    上一篇博文记录了再springboot项目中读取属性文件中配置的属性,本文学习在springboot项目中记录日志. 日志记录在项目中是很常见的一个功能了,对排查问题有很大帮助,也可以做分类分析及统计 ...

  7. springboot 入门三- 读取配置信息二(读取属性文件方式)

    在上篇文章中简单介绍自带读取方式.springboot提供多种方式来读取 一.@ConfigurationProperties(value="my") 支持更灵活的绑定及元数据的支 ...

  8. Springboot入门-日志框架配置(转载)

    默认情况下,Spring Boot会用Logback来记录日志,并用INFO级别输出到控制台. Logback是log4j框架的作者开发的新一代日志框架,它效率更高.能够适应诸多的运行环境,同时天然支 ...

  9. SpringBoot入门基础

    目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...

随机推荐

  1. POJ 1122.FDNY to the Rescue! Dijkstra

    FDNY to the Rescue! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2808   Accepted: 86 ...

  2. sc start service 1063 1053 错误原因

    在进入点函数里面要完成ServiceMain的初始化,准确点说是初始化一个SERVICE_TABLE_ENTRY结构数组,这个结构记录了这个服务程序里面所包含的所有服务的名称和服务的进入点函数,下面是 ...

  3. 2018.10.14 bzoj4571: [Scoi2016]美味(主席树)

    传送门 自认为是一道思想很妙的题. 直接分析问题. 如果没有xxx的干扰直接上可持久化01trie01trie01trie走人. 但现在有了xxx这个偏移量. 相当于把整个01trie01trie01 ...

  4. 2081.09.22 Kuma(非旋treap)

    描述 有N张卡片,编号从0到n-1, 刚开始从0到n-1按顺序排好. 现有一个操作, 对于p. l,表示从第p张卡片之后的l张卡片拿到 最前面. 例如n=7的时候, 刚开始卡片序列为0 1 2 3 4 ...

  5. 2018.07.03 POJ 2318 TOYS(二分+简单计算几何)

    TOYS Time Limit: 2000MS Memory Limit: 65536K Description Calculate the number of toys that land in e ...

  6. 2018.09.07 codeforces311B. Cats Transport(斜率优化dp)

    传送门 斜率优化dp好题. 对于第i只猫,显然如果管理员想从出发开始刚好接到它,需要在t[i]=h[i]−dist(1,i)" role="presentation" s ...

  7. 2018.07.06 POJ2536 Gopher II(二分图匹配)

    Gopher II Time Limit: 2000MS Memory Limit: 65536K Description The gopher family, having averted the ...

  8. HDU 2393 Higher Math (判断直角三角形)

    题意:给定三个边,判断是不是直角三角形. 析:水题,勾股定理... 代码如下: #include <iostream> #include <cstdio> #include & ...

  9. JavaNIO学习一

    文章来源:http://cucaracha.iteye.com/blog/2041847 对文件来说,可能最常用的操作就是创建.读取和写出.NIO.2 提供了丰富的方法来完成这些任务.本文从简单的小文 ...

  10. LDA汇总

    1.Blei的LDA代码(C):http://www.cs.princeton.edu/~blei/lda-c/index.html2.D.Bei的主页:http://www.cs.princeton ...