57. Spring 自定义properties升级篇【从零开始学Spring Boot】

 

【视频 & 交流平台】

à SpringBoot视频

http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à SpringCloud视频

http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à Spring Boot源码

https://gitee.com/happyangellxq520/spring-boot

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

 

 

之前在两篇文章中都有简单介绍或者提到过 自定义属性的用法:

 

25Spring Boot使用自定义的properties【从零开始学Spring Boot 

 

51. spring boot属性文件之多环境配置【从零开始学Spring Boot 

 

但是在实际开发过程中有更复杂的需求,我们在对properties进一步的升华。在本篇博客中您将会学到如下知识(这节中有对之前的知识的温故,对之前的升华):

(1) 在application.properties文件中添加自定义属性(单个属性使用);

(2) 在application.properties文件中添加自定义属性(多个属性使用);

(3) 配置数组注入;

(4) 松散的绑定;

(5) 参数的引用;

(6) 随机数;

(7) 使用自定义的配置文件company.properties怎么操作;

(8) 在方法上使用@Bean的时候如何进行注入;

(9) 自定义结构;

(10) 校验; 

 

 

好了,本文大纲就这么多,那么我一起来看看每个知识点都应该怎么去操作吧。

 

(1) application.properties文件中添加自定义属性(单个属性使用)

       在这里我们新建一个maven java project进行测试,取名为:spring-boot-hello4

pom.xml基本的spring boot 配置,主要用到的一个核心依赖是:

<dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-configuration-processor</artifactId>
          <optional>true</optional>
</dependency>

 

官方中对于spring-boot-configuration-processor是这么说明的:

通过使用spring-boot-configuration-processor jar, 你可以从被@ConfigurationProperties注解的节点轻松的产生自己的配置元数据文件。该jar包含一个在你的项目编译时会被调用的Java注解处理器。想要使用该处理器,你只需简单添加spring-boot-configuration-processor依赖。

好了,官方已经说得很清楚了,这个依赖主要可以在代码中轻松的使用@ConfigurationProperties注解注入属性文件配置的属性值。

单属性注入的比较简单,只需要在application.properties加入配置,如下:

#key = value的形式;
filePathLocation = d:/data/files

 

那么在对应需要使用的类中使用如下代码进行引入:

@Value("${filePathLocation}")
private String filePathLocation;

 

这里使用@Value注解就可以为我们的变量filePathLocation设置上我们在application.properties文件中设置的key值了。

在实际开发中可能我们期望的是,如果没有设置key的话,设置一个默认值,使用如下代码即可实现(以上@Value的使用方式如果在没有设置key的话是会抛出异常的):

 

@Value("${filePathLocation1:d:/data/myfiles}")
private String filePathLocation1;

 

这里的filePathLocation1我们并没有在application.properties文件中进行指定,但是查看打印信息是可以看到我们设置的默认值的,所以设置默认值的方式就是:

@Value(“${key:defaultVlaue}”) 的形式进行设置。

 

(2) application.properties文件中添加自定义属性(多个属性使用)

       多属性的设置也可以属性单属性的注入方式,但是这种方式不好,那么怎么比较优雅的注入多个属性值进行使用了。假设我们在application.properties定义了如下的属性:

#公司简称;

com.kfit.company.name =知远信科

#公司位置;

com.kfit.company.location =北京海淀区

#公司联系方式;

com.kfit.company.mobile = 110****1195

#公司员工人数;

com.kfit.company.employCount = 100

 

接下来我们定义一个ComapnyProperties类进行设置这些参数。

package com.kfit.properties;

 

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

 

//prefix设置key的前缀;

@ConfigurationProperties(prefix = "com.kfit.company")

@Component

public class CompanyProperties {

   

    private String name;

    private String location;

    private String mobile;

    private int employCount;

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public String getLocation() {

       return location;

    }

    public void setLocation(String location) {

       this.location = location;

    }

    public String getMobile() {

       return mobile;

    }

    public void setMobile(String mobile) {

       this.mobile = mobile;

    }

    public int getEmployCount() {

       return employCount;

    }

    public void setEmployCount(intemployCount) {

       this.employCount = employCount;

    }

    @Override

    public String toString() {

       return "CompanyProperties [name=" + name + ", location=" + location + ", mobile=" + mobile + ", employCount="

              + employCount + "]";

    }

}

 

那么之后我们就可以使用如下代码注入到要使用的这些属性的类进行使用了:

 

@Autowired
private CompanyProperties companyProperties;

 

 

这里需要注意下:

第一:我们使用了@ConfigurationProperties(prefix = "com.kfit.company") 快速注入我们的属性,这里prefixkey的公共部分。

第二:这里我们使用@Component 注解为spring 管理的类,那么在别的类才可以进行注入使用。

第三:在之前的文章中我们并没有使用@Component进行注册为spring 容器中,而是使用了@EnableConfigurationProperties({WiselySettings.class})  这样的方式进行注入的。这两种方式都可以。

 

(3) 配置数组注入;

我们在application.properties定义数组:

# 员工列表
com.kfit.company.employs[0]=张三
com.kfit.company.employs[1]=李四
com.kfit.company.employs[2]=王五

 

类似这样的定义那么在对应的CompanyProperties文件中怎么接收呢?很简单,定义List<String>接收就可以了,代码如下:

private List<String> employs = new ArrayList<String>();

这里的属性名称employs需要和application.properties文件的key是对应的。

这样employs注入了配置中的数据,打印为如下:

[张三, 李四, 王五]

 

(4) 松散的绑定;

Spring Boot使用宽松的规则用于绑定属性到@ConfigurationProperties beans,所以Environment属性名和bean属性名不需要精确匹配。常见的示例中有虚线分隔的(比如,context-path绑定到contextPath),环境属性大写转为小写字母(比如:PORT绑定port)。

示例:

application.properties文件中的配置:

com.kfit.company.firstName = lin
com.kfit.company.logo-path = d:/data/files/logo.png
com.kfit.company.COMPANY_FULLNAME =北京知远科技公司

 

对应的CompanyProperties类中的对应定义:

 

  //对应:com.kfit.company.firstName = lin
    private String firstName;
   
    //对应:com.kfit.company.logo-path = d:/data/files/logo.png
    private String logoPath;
   
    //对应:com.kfit.company.COMPANY_FULLNAME = 北京知远科技公司
    private String companyFullname;
   
    private List<String> employs = new ArrayList<String>();

 

看到这里,你是否终于知道为什么context-path,spring.jpa.show-sql

其实是被解释为contextPathshowSql了,不然要是指定定义一个show-sql变量是无法编译通过的,oh,原来是这么回事呢,这真是太神奇了,就是因为编程无奇不有,所以才有那么多人爱编程。

(5) 参数的引用;

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

com.kfit.blog.desc=${com.kfit.blog.name}正在写《${com.kfit.blog.title}》

 

这个就很好理解了,使用${key} 的方式进行引用。

 

(6) 随机数;

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

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

 

好了,这些在之前的文章都有介绍过了,就不多说了。

 

(7) 使用自定义的配置文件company.properties怎么操作;

如果我们自己定义一个company.properties文件,

 
#key = value的形式;
filePathLocation = d:/data/files
 
#公司简称;
com.kfit.company.name =知远信科-custom
#公司位置;
com.kfit.company.location =北京海淀区-custom
#公司联系方式;
com.kfit.company.mobile = 110****1195-custom
#公司员工人数;
com.kfit.company.employCount = 100
# 员工列表
com.kfit.company.employs[0]=张三-custom
com.kfit.company.employs[1]=李四-custom
com.kfit.company.employs[2]=王五-custom
 
com.kfit.company.firstName = lin-custom
com.kfit.company.logo-path = d:/data/files/logo.png-custom
com.kfit.company.COMPANY_FULLNAME =北京知远科技公司-custom

 

这个定义就是我们刚刚提到的一些配置,那么怎么引入了,如果使用上面的CompanyProperties的方式肯定是不行了,那么怎么呢?其实很简单,只需要在CompanyProperties稍微修改下即可,修改的地方如下:

@ConfigurationProperties(
           prefix = "com.kfit.company",
            locations="classpath:company.properties")

 

大家注意,这里唯一不一样的地方是加入了一个属性locations指定了我们要使用的配置文件路径和名称,如果我们的配置文件不在application.properties下,可以这么定义:

classpath:config/company.properties

好了这一个知识点就这么简单,只要掌握要点,一句代码就可以搞定。

 

(8) 在方法上使用@Bean的时候如何进行注入;

       这个需求点是怎么产生的呢?我们经常会配置多个数据源,那么我们有些配置还是希望从application.properties文件中进行读取,那么自然而然的在我们定义的@bean中就需要能够读取配置文件的属性。这里我们简单做个试验,我们定义CompanyProperties3,具体代码如下:

package com.kfit.properties;
 
import java.util.ArrayList;
import java.util.List;
 
public class CompanyProperties3 {
   
    private String name;
    private String location;
    private String mobile;
    private int employCount;
   
    //对应:com.kfit.company.firstName = lin
    private String firstName;
   
    //对应:com.kfit.company.logo-path = d:/data/files/logo.png
    private String logoPath;
   
    //对应:com.kfit.company.COMPANY_FULLNAME = 北京知远科技公司
    private String companyFullname;
   
    private List<String> employs = new ArrayList<String>();
   
   
    public String getFirstName() {
       return firstName;
    }
    public void setFirstName(String firstName) {
       this.firstName = firstName;
    }
    public String getLogoPath() {
       return logoPath;
    }
    public void setLogoPath(String logoPath) {
       this.logoPath = logoPath;
    }
    public String getCompanyFullname() {
       return companyFullname;
    }
    public void setCompanyFullname(String companyFullname) {
       this.companyFullname = companyFullname;
    }
    public List<String> getEmploys() {
       return employs;
    }
    public void setEmploys(List<String> employs) {
       this.employs = employs;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public String getLocation() {
       return location;
    }
    public void setLocation(String location) {
       this.location = location;
    }
    public String getMobile() {
       return mobile;
    }
    public void setMobile(String mobile) {
       this.mobile = mobile;
    }
    public int getEmployCount() {
       return employCount;
    }
    public void setEmployCount(intemployCount) {
       this.employCount = employCount;
    }
    @Override
    public String toString() {
       return "CompanyProperties [name=" + name + ", location=" + location + ", mobile=" + mobile + ", employCount="
              + employCount + ", firstName=" + firstName + ", logoPath=" + logoPath + ", companyFullname="
              + companyFullname + ", employs=" + employs + "]";
    }
}

 

注意这里的代码和以上不一样的是类上的注解全没有了,之后我们在App.java启动类中或者其它的类也是可以的,使用@Bean的方式进行注入。

    @Bean

    @ConfigurationProperties(prefix = "com.kfit.company")

    public CompanyProperties3 companyProperties3(){

       returnnew CompanyProperties3();

    }

那么在其它的类中我们就使用@Autowired进行注入使用了,如下:

@Autowired
private CompanyProperties3 companyProperties3;

 

是不是很好玩呢。

 

(9) 自定义结构;

       对于复杂的配置或嵌套的kv,我们可以编写自定义结构属性以更好的方式进行管理。

比如我们在application.properties文件中有如下信息:

com.kfit.employForzs.name =张三
com.kfit.employForzs.age = 20
com.kfit.employForzs.gender =男
 
com.kfit.employForls.name =李四
com.kfit.employForls.age = 25
com.kfit.employForzs.gender =女

 

com.kfit.properties.CompanyEmployee的代码如下:

package com.kfit.properties;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
//prefix设置key的前缀;
@ConfigurationProperties(prefix = "com.kfit")
@Component
public class CompanyEmployee {
   
    private CompanyEmployeeInfo employForzs;
   
    private CompanyEmployeeInfo employForls;
   
    public CompanyEmployeeInfo getEmployForzs() {
       return employForzs;
    }
 
    publicvoid setEmployForzs(CompanyEmployeeInfo employForzs) {
       this.employForzs = employForzs;
    }
 
    public CompanyEmployeeInfo getEmployForls() {
       return employForls;
    }
 
    publicvoid setEmployForls(CompanyEmployeeInfo employForls) {
       this.employForls = employForls;
    }
 
    public static class CompanyEmployeeInfo {
       private String name;
       private int age;
       private String gender;
       public String getName() {
           return name;
       }
       publicvoid setName(String name) {
           this.name = name;
       }
       public int getAge() {
           return age;
       }
       public void setAge(intage) {
           this.age = age;
       }
       public String getGender() {
           returngender;
       }
       public void setGender(String gender) {
           this.gender = gender;
       }
       @Override
       public String toString() {
           return "EmployForzs [name=" + name + ", age=" + age + ", gender=" + gender + "]";
       }
    }
 
    @Override
    public String toString() {
       return "CompanyEmployee [employForzs=" + employForzs + ", employForls=" + employForls + "]";
    }
}

 

观察以上的代码我们定义了一个内部静态类进行处理相同的属性,那么在外部类中定义两个变量进行接收application.properties文件中的配置信息。

之后在其它类就可以使用@Autowired进行注入使用了。

 

(10) 校验;

       当我们使用@ConfigurationProperties的时候,我们希望对一些参数进行校验,比如有些参数为空或者数字超出的限制就抛出异常信息,那么这个怎么操作呢?

application.properties文件中加入:

com.kfit.company.url = http://www.kfit.com

CompanyProperties类中加入:

@URL

private String url;

这里使用了@URLurl进行校验,如果是非法的url在启动的时候是会抛出异常信息的。

其中@URL对应的包路径为:org.hibernate.validator.constraints.URL

那么还有其它的什么校验器呢?看下文:

@Max(value = 99)

private int employCount;

定义最大值只能是99,那么如果运行的话,显然就会报错了,因为之前我们配置的值是100,那么就会看到控制台抛出异常信息:

default message [最大不能超过99]

这里只是截取了一小部分异常信息,具体的异常信息是可以参数那个参数的设置有问题的。

既然有最大值就有最小值的配置:

@Max(value = 1000)

@Min(value = 1)

private int employCount;

接着往下看:

@NotNull

private String name;

@NotNull说明name不能为null,如果为null就抛出异常。

接着往下看:

@NotEmpty

private String location;

@NotEmpty不能为空,当没有定义keykey的值为空字符的时候都会抛出异常信息。

 

validation-api下包javax.validation.constraints下还有其它的校验器,大家可以根据需要自行学习。当然校验器是可以自定定义的,大家可以自己在扩展下,好了这个章节就介绍到这里了。

 

Spring Boot 系列博客】

视频&交流平台

à Spring Boot网易云课堂视频

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

网易云课堂视频最新更新

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

3、Spring Boot日志-log4j2

第十二章 Spring Boot 知识点2

1、spring boot 服务配置和部署

2、Spring Boot 定制URL匹配规则

 

 

历史章节

 

第一章 快速开始

1、Spring Boot之Hello World

2、Spring Boot之Hello World访问404

 

第二章 Spring Boot之JSON

1、spring boot返回json数据

2、Spring Boot完美使用FastJson解析JSON数据

 

第三章 Spring Boot热部署

1、Spring Boot热部署(springloader)

2、springboot + devtools(热部署)

 

第四章 Spring Boot数据库

1、Spring Boot JPA/Hibernate/Spring Data概念

2、Spring Boot JPA-Hibernate

3、Spring Boot Spring Data JPA介绍

4、Spring Boot JdbcTemplate

5、Spring Boot集成MyBatis

 

第五章 web开发

1、全局异常捕捉

2、配置server信息

3、spring boot使用thymeleaf

4、Spring Boot 使用freemarker

5、Spring Boot添加JSP支持

 

第六章 定时任务

1、Spring Boot定时任务

2、Spring Boot 定时任务升级篇(动态修改cron参数)

3、Spring Boot 定时任务升级篇(动态添加修改删除定时任务)

4、Spring Boot 定时任务升级篇(集群/分布式下的定时任务说明)

5、Spring Boot Quartz介绍

6、Spring Boot Quartz在Java Project中使用

7、Spring Boot 集成Quartz普通使用

8、Spring Boot 集成Quartz升级版

9、Spring Boot 集成Quartz二次升级版

10、Spring Boot 集成Quartz-Job如何自动注入Spring容器托管的对象

 

第七章 Spring Boot MyBatis升级篇

1、Spring Boot MyBatis升级篇-注解

2、Spring Boot MyBatis升级篇-注解-自增ID

3、Spring Boot MyBatis升级篇-注解-增删改查

4、Spring Boot MyBatis升级篇-注解-分页查询

5、Spring Boot MyBatis升级篇-注解-分页PageHelper不生效

6、Spring Boot MyBatis升级篇-注解- mybatic insert异常:BindingException: Parameter 'name' not found

7、Spring Boot MyBatis升级篇-注解- #和$符号特别篇

8、Spring Boot MyBatis升级篇-注解-@Result

9、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案一:<script>

10、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案二:@Provider

11、Spring Boot MyBatis升级篇-注解-动态SQL-参数问题

12、Spring Boot MyBatis升级篇-注解-特别篇:@MapperScan和@Mapper

13、Spring Boot MyBatis升级篇-XML

14、Spring Boot MyBatis升级篇-XML-自增ID

15、Spring Boot MyBatis升级篇-XML-增删改查

16、Spring Boot MyBatis升级篇-XML-分页查询

17、Spring Boot MyBatis升级篇-XML-分页PageHelper不生效

18、Spring Boot MyBatis升级篇-XML-动态SQL(if test)

19、Spring Boot MyBatis升级篇-XML-注解-初尝试

20、Spring Boot MyBatis升级篇- pagehelper替换为pagehelper-spring-boot-starter

 

第八章 Spring Boot 知识点1

1、Spring Boot 拦截器HandlerInterceptor

2、Spring Boot启动加载数据CommandLineRunner

3、Spring Boot环境变量读取和属性对象的绑定

4、Spring Boot使用自定义的properties

5、Spring Boot使用自定义的properties

6、Spring Boot使用@SpringBootApplication

7、Spring Boot 监控和管理生产环境

 

第十章 Spring Boot 打包部署

1、Spring Boot打包部署((提供Linux的sh文件))

 

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

 

3、Spring Boot日志-log4j2

 

更多查看博客: http://412887952-qq-com.iteye.com/

 

猜你喜欢

转载自412887952-qq-com.iteye.com/blog/2311017