自定义属性与加载

我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义:

application-dev.yml

com.didispace.blog:
name: 程序猿DD
title: Spring Boot教程
desc: ${com.didispace.blog.name}正在努力写《${com.didispace.blog.title}》
# 随机字符串
value: ${random.value}
# 随机int
number: ${random.int}
# 随机long
bignumber: ${random.long}
# 10以内的随机数
test1: ${random.int(10)}
# 10-20的随机数
test2: ${random.int[10,20]}

  

package com.wls.integrateplugs.property;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; /**
* @author 程序猿DD
* @version 1.0.0
* @date 16/5/5 下午12:16.
* @blog http://blog.didispace.com
*/
@Component
public class BlogProperties { @Value("${com.didispace.blog.name}")
private String name;
@Value("${com.didispace.blog.title}")
private String title;
@Value("${com.didispace.blog.desc}")
private String desc; @Value("${com.didispace.blog.value}")
private String value;
@Value("${com.didispace.blog.number}")
private Integer number;
@Value("${com.didispace.blog.bignumber}")
private Long bignumber;
@Value("${com.didispace.blog.test1}")
private Integer test1;
@Value("${com.didispace.blog.test2}")
private Integer test2; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} public String getValue() {
return value;
} public void setValue(String value) {
this.value = value;
} public Integer getNumber() {
return number;
} public void setNumber(Integer number) {
this.number = number;
} public Long getBignumber() {
return bignumber;
} public void setBignumber(Long bignumber) {
this.bignumber = bignumber;
} public Integer getTest1() {
return test1;
} public void setTest1(Integer test1) {
this.test1 = test1;
} public Integer getTest2() {
return test2;
} public void setTest2(Integer test2) {
this.test2 = test2;
}
}

  

按照惯例,通过单元测试来验证BlogProperties中的属性是否已经根据配置文件加载了。然后通过@Value("${属性名}")注解来加载对应的配置属性,具体如下:

单元测试

package com.wls.test.integrateplugs.property;

import com.wls.integrateplugs.property.BlogProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
*
* @author 程序猿DD
* @version 1.0.0
* @blog http://blog.didispace.com
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class BlogPropertiesTest { private static final Log log = LogFactory.getLog(BlogPropertiesTest.class); @Autowired
private BlogProperties blogProperties; @Test
public void test1() throws Exception {
Assert.assertEquals("程序猿DD", blogProperties.getName());
Assert.assertEquals("Spring Boot教程", blogProperties.getTitle());
Assert.assertEquals("程序猿DD正在努力写《Spring Boot教程》", blogProperties.getDesc()); log.info("随机数测试输出:");
log.info("随机字符串 : " + blogProperties.getValue());
log.info("随机int : " + blogProperties.getNumber());
log.info("随机long : " + blogProperties.getBignumber());
log.info("随机10以下 : " + blogProperties.getTest1());
log.info("随机10-20 : " + blogProperties.getTest2()); } }

  

参数间的引用

application.properties中的各个参数之间也可以直接引用来使用,就像下面的设置:

 

com.didispace.blog.name=程序猿DD
com.didispace.blog.title=Spring Boot教程
com.didispace.blog.desc=${com.didispace.blog.name}正在努力写《${com.didispace.blog.title}》

  

com.didispace.blog.desc参数引用了上文中定义的nametitle属性,最后该属性的值就是程序猿DD正在努力写《Spring Boot教程》

使用随机数

在一些情况下,有些参数我们需要希望它不是一个固定的值,比如密钥、服务端口等。Spring Boot的属性配置文件中可以通过${random}来产生int值、long值或者string字符串,来支持属性的随机值。

# 随机字符串
com.didispace.blog.value=${random.value}
# 随机int
com.didispace.blog.number=${random.int}
# 随机long
com.didispace.blog.bignumber=${random.long}
# 10以内的随机数
com.didispace.blog.test1=${random.int(10)}
# 10-20的随机数
com.didispace.blog.test2=${random.int[10,20]}

  

通过命令行设置属性值

相信使用过一段时间Spring Boot的用户,一定知道这条命令:java -jar xxx.jar --server.port=8888,通过使用–server.port属性来设置xxx.jar应用的端口为8888。

在命令行运行时,连续的两个减号--就是对application.properties中的属性值进行赋值的标识。所以,java -jar xxx.jar --server.port=8888命令,等价于我们在application.properties中添加属性server.port=8888,该设置在样例工程中可见,读者可通过删除该值或使用命令行来设置该值来验证。

通过命令行来修改属性值固然提供了不错的便利性,但是通过命令行就能更改应用运行的参数,那岂不是很不安全?是的,所以Spring Boot也贴心的提供了屏蔽命令行访问属性的设置,只需要这句设置就能屏蔽:SpringApplication.setAddCommandLineProperties(false)

多环境配置

我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。

对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,Spring Boot也不例外,或者说更加简单。

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

  • application-dev.properties:开发环境
  • application-test.properties:测试环境
  • application-prod.properties:生产环境

至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

如:spring.profiles.active=test就会加载application-test.properties配置文件内容

下面,以不同环境配置不同的服务端口为例,进行样例实验。

  • 针对各环境新建不同的配置文件application-dev.propertiesapplication-test.propertiesapplication-prod.properties

  • 在这三个文件均都设置不同的server.port属性,如:dev环境设置为1111,test环境设置为2222,prod环境设置为3333

  • application.properties中设置spring.profiles.active=dev,就是说默认以dev环境设置

  • 测试不同配置的加载

    • 执行java -jar xxx.jar,可以观察到服务端口被设置为1111,也就是默认的开发环境(dev)
    • 执行java -jar xxx.jar --spring.profiles.active=test,可以观察到服务端口被设置为2222,也就是测试环境的配置(test)
    • 执行java -jar xxx.jar --spring.profiles.active=prod,可以观察到服务端口被设置为3333,也就是生产环境的配置(prod)

按照上面的实验,可以如下总结多环境的配置思路:

  • application.properties中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认配置
  • application-{profile}.properties中配置各个环境不同的内容
  • 通过命令行方式去激活不同环境的配置

Spring Boot☞ 配置文件详解:自定义属性、随机数、多环境配置等的更多相关文章

  1. Spring Boot 配置文件详解

    Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...

  2. Spring Boot配置文件详解:自定义属性、随机数、多环境配置

    自定义属性与加载 我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义: application-dev.yml com.didispace.blog: ...

  3. (转) SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

    springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...

  4. SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot2-config-file/ 本文出自方志朋的博客 ...

  5. 史上最全的Spring Boot配置文件详解

    Spring Boot在工作中是用到的越来越广泛了,简单方便,有了它,效率提高不知道多少倍.Spring Boot配置文件对Spring Boot来说就是入门和基础,经常会用到,所以写下做个总结以便日 ...

  6. Spring Boot配置文件详解-ConfigurationProperties和Value优缺点-(转)好文

    文章转自 http://www.cnblogs.com/itdragon/p/8686554.html Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们 ...

  7. Spring boot 配置文件详解 (properties 和yml )

    从其他框架来看 我们都有自己的配置文件, hibernate有hbm,mybatis 有properties, 同样, Spring boot 也有全局配置文件. Springboot使用一个全局的配 ...

  8. Spring boot 入门二:Spring Boot配置文件详解

    一.自定义属性 当我们创建一个springboot项目的时候,系统默认会为我们在src/main/java/resources目录下创建一个application.properties.同时也支持ym ...

  9. Spring Boot 配置文件详解:Properties和YAML

    一.配置文件的生效顺序,会对值进行覆盖: 1. @TestPropertySource 注解 2. 命令行参数 3. Java系统属性(System.getProperties()) 4. 操作系统环 ...

随机推荐

  1. 学习FPGA,踏上一步台阶

    学习FPGA的过程中,要想踏上一步台阶,需要注意一下几点: 时序约束的原因和使用方法,能熟练正确的应用最基本的时钟周期约束,时序例外约束,异步时钟域约束,同步复位的约束,高扇出约束. 清楚FPGA芯片 ...

  2. Python库-BeautifulSoup

    sp = BeautifulSoup.bs4.BeautifulSoup(html.text,"html.parser") 方法 1.sp.title 返回网页标题 2.sp.te ...

  3. 如何配置Python环境

    (1) 下载:请在Python官网下载页面(https://www.python.org/downloads/)选择合适的版本(建议选择3.5.2版)的链接,在该版本的下载页面选择合适的安装文件:64 ...

  4. xshell连接kali

    连接出现错误,连接不上去,看到一篇文章可以使用,https://blog.csdn.net/yemaxq/article/details/78171241

  5. codechef January Lunchtime 2017简要题解

    题目地址https://www.codechef.com/LTIME44 Nothing in Common 签到题,随便写个求暴力交集就行了 Sealing up 完全背包算出得到长度≥x的最小花费 ...

  6. 第一章 安装ubuntu

    最近正在研究hadoop,hbase,准备自己写一套研究的感研,下面先讲下安装ubuntu,我这个是在虚拟机下安装,先用 文件转换的方式安装. 1:选择语言:最好选择英文,以免出错的时候乱码 2:选择 ...

  7. B2B,ERP,SCM

    B2B: B2B电子商务平台是指一个市场的领域的一种,是企业对企业之间的营销关系.电子商务是现代B2B marketing的一种具体主要的表现形式.网商通过它将企业内部网,通过B2B网站与客户紧密结合 ...

  8. Python模块学习 ---- datetime

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于time模块, ...

  9. Python实践练习:生成随机的测验试卷文件

    题目 假如你是一位地理老师,班上有 35 名学生,你希望进行美国各州首府的一个小测验.不妙的是,班里有几个坏蛋,你无法确信学生不会作弊.你希望随机调整问题的次序,这样每份试卷都是独一无二的,这让任何人 ...

  10. leetcode558

    """ # Definition for a QuadTree node. class Node(object): def __init__(self, val, isL ...