SpringBoot 2.X课程学习 | 第六篇:挖掘配置文件的秘密

 

1、两种配置文件获取值的方式

        由于广泛属性比较简单,可能复杂属性有些小伙伴们不知道怎么获取。所以我将application.yml和application.properties两种配置文件中对象复杂属性定义的内容列举出来:java

        application.properties文件:web

#自定义list集合
person.lists=a,b,c
#自定义map集合
person.maps.k1=va2
person.maps.k2=v2
#自定义person中的dog对象属性
person.dog.name=毒瓦斯
person.dog.age=8

       application.yml文件:spring

#自定义map集合
#缩进方式
person:
  maps:
    k1: v2
    k2: v3

#行内方式
person: 
  maps:{k1: v2,k2: v3}


#自定义数组
#缩进方式
person
  lists:
    - 镰刀湾
    - 瓦斯的

#行内方式
person:
  lists:[镰刀湾,瓦斯的]

#自定义对象属性
person:
  dog:
    name: awdwa
    age: 11

     一、方式一  经过@ConfigurationProperties注解     

            @ConfigurationProperties注解做用是告诉springboot将本类中的全部属性与配置文件中的属性进行绑定,它须要提供prefix属性,可经过统一前缀批量将类中属性和配置文件中属性进行绑定,使用该方式的时候须要引入配置文件处理器依赖:数组

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

           而且使用添加@Component、@Configuration或者@EnableConfigurationProperties注解,将类做为容器中的组件,才能使用容器中的功能。 springboot

       示例代码以下:app

package com.example.demo.entity;

import org.springframework.stereotype.Component;

@Component
@Data
public class Dog {

    private String name;

    private int age;

}

          1.一、使用@Component注解

package com.example.demo.entity;


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

import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {

    private Map<String,Object> maps;

    private List<Object> lists;

    private Dog dog;
}

      1.二、使用@configuration注解:

package com.example.demo.entity;


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

import java.util.List;
import java.util.Map;

@configuration
@ConfigurationProperties(prefix = "person")
@Data
public class Person {

    private Map<String,Object> maps;

    private List<Object> lists;

    private Dog dog;
}

      1.三、使用@EnableConfigurationProperties注解

package com.example.demo.entity;


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

import java.util.List;
import java.util.Map;

@ConfigurationProperties(prefix = "person")
@Data
public class Person {

    private Map<String,Object> maps;

    private List<Object> lists;

    private Dog dog;
}
@SpringBootApplication
@EnableConfigurationProperties(Person.class)
public class DemoApplication {

    @Autowired
    private Person person;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello() {
        System.out.println("person" + person);
        return "hello world";
    }

    public static void main(String[] args) {
        //SpringApplication.run(TestProperty1.class, args);
        new SpringApplicationBuilder(DemoApplication.class).web(SERVLET).run(args);

    }
}

二、方式二 经过@Value注解

          @Value注解相似在原始spring项目在配置文件中配置bean标签,在bean标签中配置了properties标签,并提供value属性值,例如:dom

<bean class="xxx">
  <properties name="xxx" value=""/>
</bean>

@Value注解至关于这个properties标签中的value属性值。

         使用@Value注解不能经过前缀批量将类中全部属性与配置文件中特定属性绑定,它支持使用${key}(从环境变量、配置文件中获取值)、#{SpEl}等。函数

         好比:spring-boot

@Value("${person.last-name}")
    private String name;

    @Value("#{11*2}")  /**为spring表达式
    private int age;

    @Value("true")
    private boolean flag;

      将配置文件中的属性绑定到类中的属性时,对于对象属性,若是键包含除小写字母数字字符或-之外的任何字符,则须要使用括号表示法,以便保留原始值。若是键未被[]包围,则将删除任何非字母数字或-的字符。好比map属性:ui

#yml文件写法
#自定义map集合
person:
  maps:
    "[/key1]": value1
    /key3: value3
    k2: v2


#properties文件写法
#自定义map集合
person.maps.[/key1]=va2
person.maps./key3=v2

那么使用注解获取到的map集合内容为maps={/key1=va2, key3=v2}

2、@ConfigurationProperties和@Value注解不一样

  @ConfigurationProperties  @Value
功能 批量注入配置文件中的属性 只能一个一个的注入
松散绑定(Relaxed Binding) 支持 不支持
SrEl表达式 不支持 支持
JSR303数据检验 支持 不支持
复杂类型封装 支持 不支持
绑定类属性是否提供set、get方法

2.一、松散绑定

         好比类中定义了一个属性,属性名为firstName,若是使用@ConfigurationProperties,那么配置文件中,该属性能够写成以下形式:

person.firstName:使用标准方式
person.first-name:大写用-,建议在.properties和.yml文件中使用。
person.first_name:大写用_,它是用于.properties和.yml文件的可选格式。
PERSON.FIRST_NAME  大写格式,建议在使用系统环境变量时使用。

        可是若是使用@Value注解,那么只能写成@Value("${person.firstName}")。

        每一个属性源的宽松绑定规则:

属性源 Simple List
Properties Files Camel case(驼峰式), kebab case(短横线), or underscore notation(下划线符号) 使用[]或逗号分隔值的标准列表语法
YAML Files Camel case(驼峰式), kebab case(短横线), or underscore notation(下划线符号) 标准yaml列表语法或逗号分隔值

Environment Variables

(环境变量)

如下划线做为分隔符的大写格式。“-”不该在属性名中使用 由下划线包围的数字值,例如my_Acme_1_Other=my.Acme[1].Other

System properties

(系统属性)

Camel case(驼峰式), kebab case(短横线), or underscore notation(下划线符号) 使用[]或逗号分隔值的标准列表语法

2.二、JSR303数据校验

        @ConfigurationProperties注解支持JSR303数据校验,意味着进行数据校验的属性必须符合规则才给予经过,不然报错。好比给类中的某个属性添加@Email注解,则规定,该属性值必须符合邮箱格式才进行经过。查看代码:

@Validated
public class Person {

    @Value("${person.last-name}")
    @Email
    private String name;

}

2.三、关于绑定类中的属性是否须要提供set、get方法

   这个状况只适应于使用@ConfigurationProperties注解。因为绑定是经过标准javaBean属性,所以绝大状况下属性都须要提供setter和getter方法,除如下状况能够省略setter方法:

  • 集合或者数组 能够经过索引(一般使用yaml)或使用单个逗号分隔值(属性)访问。对于数组属性,setter方法是强制的。咱们建议始终为此类类型添加setter方法。若是初始化集合,请确保它不是不可变的。
  • 初始化了嵌套的POJO属性,例如:person类中有一个属性:private Dog dog=new Dog();则不须要setter方法,可是是private Dog dog;则须要提供setter方法。若是但愿绑定器使用其默认构造函数动态建立实例,则须要一个setter方法。
  • 最后,只考虑标准的JavaBean属性,不支持对静态属性的绑定。

3、配置文件占位符

      springboot配置文件中容许使用随机数或者在配置文件中引用前面配置过的属性来做为占位符。

      好比使用随机数的占位符:

${random.int}
${random.long}
${random.value}
${random.uuid}
${random.int(10)}
${random.int[1024,65523]}

     好比在文件中引用前面配置过的属性做为占位符:

app.name=MyApp
app.decription=${app.name} is a SpringBoot Application

   若是配置文件上下文没有该属性时,咱们随便输入一个属性名,那么它将会把这个属性名打印出来,好比:

app.name=Myapp
app.decription=${MyProject} is a SpringBoot Application


那么使用注解获取app.decription这个属性值时,打印的结果为:MyProject is a SpringBoot Application

     咱们还能够为这个配置文件上下文都没有的属性值赋值。好比以下操做:

app.name=Myapp
app.decription=${MyProject:customProject} is a SpringBoot Application


那么使用注解获取app.decription这个属性值时,打印的结果为:customProject is a SpringBoot Application

4、配置文件加载顺序

相关文章
相关标签/搜索