深入Spring Boot (三):Properties属性配置文件使用详解

2018年3月1日Spring官网正式对外发布了Spring Boot2.0正式版,新版本新增了很多新特性,使用上会更加便捷,所以《深入Spring Boot》系列文章将会以2.0正式版作为基础。

Spring Boot弱化配置的特性让属性配置文件的使用也更加便捷,它默认支持对application.properties或application.yml属性配置文件处理,即在application.properties或application.yml文件中添加属性配置,可以使用@Value注解将属性值注入到beans中,或使用@ConfigurationProperties注解将属性值绑定到结构化的beans中,本篇将详细介绍Properties属性配置文件的使用。
本篇主要包含以下8部分内容:

  1. 基础使用;
  2. 配置随机值;
  3. 命令行设置属性;
  4. 属性间引用;
  5. 多环境支持;
  6. 自定义配置文件名及位置;
  7. 加载更多配置;
  8. 使用@ConfigurationProperties注解。

1.基础使用

在resources目录下新增application.properties文件并添加如下属性配置。

server.display.name=app
server.address=127.0.0.1

新增Properties.java,并使用@Value注解将属性值注入到对应属性上。

@Component
public class Properties {
    @Value("${server.display.name}")
    private String serverDisplayName;
    @Value("${server.address}")
    private String serverAddress;
    //省略getter和setter
}

编写测试类PropertiesTest.java,验证属性注入是否成功。

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class PropertiesTest {
    @Autowired
    private Properties properties;
    @Test
    public void test() {
        System.out.println("server display name:" + properties.getServerDisplayName());
        System.out.println("server address:" + properties.getServerAddress());
    }
}

执行单元测试test方法,输出结果如下。

server display name:app
server address:127.0.0.1

2. 配置随机值

properties属性配置文件支持使用随机数,例如可以使用如下一些随机数配置。

#注入一个给定字节的MD5摘要的十六进制字符串,结果包含32个字符
my.secret=${random.value}
#注入一个int类型伪随机数
my.number=${random.int}
#注入一个long类型伪随机数
my.bignumber=${random.long}
#注入一个10以内的int类型伪随机数
my.number.less.than.ten=${random.int(10)}
#注入一个在1024到65536以内的int类型伪随机数
my.number.in.range=${random.int[1024,65536]}

随机数的值是在Spring容器启动的过程中通过RandomValuePropertySource产生的,具体代码如下图。
这里写图片描述

3. 命令行设置属性

Spring Boot还可以通过命令行参数向application.properties中添加属性配置,例如执行java -jar test.jar –server.port=9000,将向application.properties中新增一个属性配置,当然如果要屏蔽这种属性添加方式,可以添加如下代码实现。

SpringApplication.setAddCommandLineProperties(false);

4. 属性间引用

Spring Boot的properties属性配置同样支持属性间引用,例如:

server.display.name=app
server.address=127.0.0.1
server.full.display.name=${server.address}-${server.display.name}

5.多环境支持

Spring-Boot同样支持不同环境的属性配置文件切换,通过创建application-{profile}.properties文件,其中{profile}是具体的环境标识名称,例如:
- application-dev.properties用于开发环境;
- application-test.properties用于测试环境;
- application-uat.properties用于uat环境;

如果要想使用application-dev.properties文件,则在application.properties文件中添加spring.profiles.active=dev;如果要想使用application-test.properties文件,则在application.properties文件中添加spring.profiles.active=test。

这种配置文件切换提供了一定的便捷性,但是,如果使用自动集成部署工具,不可能每次打包部署都手动修改application.properties中spring.profiles.active的值,所以,一般的做法是在项目路径下建立以环境标识名称为名的目录,然后每个目录下添加各自环境的application.properties属性配置,在自动打包部署的时候将对应的application.properties拷贝到classpath下,这样就实现了配置文件切换。

6. 自定义配置文件名及位置

以上对application.properties文件的使用都是基于在classpath根路径下,即将application.properties文件放在resources目录下。Spring Boot支持从以下位置加载application.properties文件:
当前目录下的/config子目录;
当前目录;
classpath下的/config包;
classpath根路径。
下面用一张图展示这四个位置。
这里写图片描述
若这四个位置都存在application.properties文件,属性值的覆盖顺序是:1>2>3>4,例如四个位置的application.properties文件都配置了db.name属性,最终生效的是当前目录下的/config子目录application.properties文件中的属性值;如果四个位置的application.properties文件,只有classpath下的/config包application.properties文件配置了db.name,最终生效的就是这个位置下的属性值。

如果不喜欢将application.properties作为配置文件名,可以通过指定spring.config.name环境属性来更改它的名称,可以将spring.config.name设置为系统属性或命令行参数,但是需要注意多数操作系统的key名称不允许以句号分割,可以使用SPRING_CONFIG_NAME代替spring.config.name,例如将名称改为app.properties,可以使用如下命令行参数:

java -jar spring-boot-2.jar --spring.config.name=app

Spring Boot也支持自主指定配置文件的位置,可以使用spring.config.location环境属性引用一个明确的路径(目录位置或文件路径列表以逗号分割),同样可以将spring.config.location设置为系统属性或命令行参数,key名同样也需要使用下划线代替句号分隔符,例如指定db.properties和mq.properties,可以使用如下命令行参数:

java -jar spring-boot-2.jar --spring.config.location=classpath:/db.properties,classpath:/mq.properties

7. 加载更多配置

项目的属性配置文件比较多的时候,会把它们按用途分为多个配置文件,例如application-db.properties、application-mq.properties等,Spring Boot也支持对这些文件的加载,除了使用spring.config.location实现,还可以在application.properties中添加spring.profiles.include属性实现,属性值有多个的使用逗号分隔,例如额外加载application-db.properties和application-mq.properties配置如下:

spring.profiles.include=db,mq

8. 使用@ConfigurationProperties注解

虽然使用@Value注解可以很好的把属性配置文件中的值注入到beans中,但是,当属性配置文件变多或属性特别多的时候,使用@Value注解将变的很麻烦,而@ConfigurationProperties注解可以很轻松的实现属性注入。例如有如下属性配置:

db.ip=127.0.0.1
db.port=3306

在编写属性注入bean时,只需要使用@ConfigurationProperties注解和prefix属性,prefix属性值用于指定属性的前缀 ,具体代码如下:

@Component
@ConfigurationProperties(prefix = "db")
public class DbProperties {
    private String ip;
    private String port;
    //省略getter和setter
}

Spring Boot的@ConfigurationProperties注解对这种属性注入方式的key校验不是很严格,你可以在属性配置文件中配置DB.IP或DB_IP,Spring Boot都可以处理。

如果要想注入如下这些配置属性,上面的代码需要变动一下,示例代码只能注入db.xxx或db_xxx形式的配置。

db.ip=127.0.0.1
db.port=3306
db.page.min=1
db.page.max=50

变更后的代码如下:

@Component
@ConfigurationProperties(prefix = "db")
public class DbProperties {
    private String ip;
    private String port;
    private Page page;
    //省略getter和setter
}
public class Page {
    private String min;
    private String max;
    //省略getter和setter
}

按照这种处理逻辑,还可以进行更深层次的属性注入,这种处理方式很像JSON或XML的转换。

为了验证属性注入的有效性,@ConfigurationProperties可以和@Validated注解一起使用,使用JSR-303 javax.validation包下的注解,例如@NotNull等。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值