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. abort: no username supplied (see "hg help config")

    abort: no username supplied (see "hg help config") 在hg中输入commit 指令时,如果出现下述结果: $ hg commit ...

  2. 使用 Php Artisan Tinker 来调试你的 Laravel

    Posted on 2016年6月19日 by ichou 本文翻译自:Tinker with the Data in Your Laravel Apps with Php Artisan Tinke ...

  3. Zookeeper 系列(四)ZKClient API

    Zookeeper 系列(四)ZKClient API 环境准备: <dependency> <groupId>com.101tec</groupId> <a ...

  4. msys2 显示git branch

    在.bashrc或.bash_profile中添加以下内容 function parse_git_branch () { git branch 2> /dev/null | sed -e '/^ ...

  5. 2018.06.29 NOIP模拟 边的处理(分治+dp)

    边的处理(side.cpp) [问题描述] 有一个 n 个点的无向图,给出 m 条边,每条边的信息形如<x,y,c,r><x,y,c,r><x,y,c,r>. 给出 ...

  6. 31. The New Bread Earners 挣钱养家的新军

    31. The New Bread Earners 挣钱养家的新军 ① They call them the new bread earners.They are women,and they are ...

  7. matplotlib在MAC系统下中文字体显示问题

    最近想把部分python数据分析的代码从win系统迁移到MAC上,有部分图片上涉及中文显示,迁移到MAC上warning: UserWarning: findfont: Font family [u' ...

  8. hive 学习之异常篇

    一.刚装上hive在执行hive启动的过程中出现 [hadoop@localhost hive-0.6.0]$ hive Invalid maximum heap size: -Xmx4096m Th ...

  9. js 面向对象 定义对象

    js面向对象看了很多,却没有完全真正的理解,总是停留在一定的阶段,这次再认真看一下. 面向对象包含两种:定义类或对象:继承机制:都是通过工厂模式,构造函数,原型链,混合方法这四个阶段,原理也一样,只是 ...

  10. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed(在64位模式下运行安装了32位的Oracle客户端组件时,会发生此问题)

    部署win服务时出现下面的问题: 在事件查看器中看到如下错误: 日志名称: Application来源: ***调度服务日期: 2014/5/21 12:53:21事件 ID: 0任务类别: 无级别: ...