SpringBoot之@ConfigurationProperties、@PropertySource注解的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fangxinde/article/details/83622248

          当获取主配置文件中属性值时,只需@ConfigurationProperties(prefix = "person")注解来修饰某类,其作用是告诉springBoot,此类中的属性将与默认的全局配置文件中对应属性一一绑定。属性名必须是application.yml或application.properties。【prefix = "person"】表示与配置文件中哪个层级的属性进行绑定。

           当一些属性不想配置到主配置文件,需自定义一个配置文件,需通过@PropertySource注解指定此配置文件路径。

而@ConfigurationProperties(prefix = "xxx")注解指定自定义配置文件中哪个层级属性需绑定。

配置文件的位置:\src\main\resources\application.yml

一、bean类与主配置文件的绑定

案例:bean类Person.java


@Component
@ConfigurationProperties(prefix = "person")//只能从默认的全局文件中获取
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
      
   //======================get 、set方法省略=============

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

配置文件:

person:
  lastName:  fang \n xin \n de
  age:  18
  boss: false
  birth: 2018/12/10
  maps: {a1: fang, a2: li,a3: zhang}
  lists: [cat,pig,dog]
  dog:
    name: xiaogou10号
    age:  1

 测试案例:

package com.atguigu.springboot;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
	@Autowired
	Person person;
	 
	@Test
	public void contextLoads() {
		System.out.println(person);
	}
}

打印结果:

Person{lastName='fang \n xin \n de', age=18, boss=false, birth=Mon Dec 10 00:00:00 CST 2018, maps={a1=fang, a2=li, a3=zhang}, lists=[cat, pig, dog], dog=Dog{name='xiaogou10号', age=1}}

二、bean类与自定义的配置文件绑定,需@ConfigurationProperties、@PropertySource两个注解一起修饰bean类

@PropertySource指定自定义配置文件的地址

@ConfigurationProperties指定绑定属性的层级


@Component
@ConfigurationProperties(prefix = "person") 
@PropertySource(value ={"classpath:person.properties"}) 
@Validated
public class Person {
     
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
//==================set、get方法省略=======================
    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

自定义配置文件person.properties:

person.lastName=李次方1110
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog01
person.dog.age=15

打印的结果:

Person{lastName='李次方1110', age=12, boss=false, birth=Fri Dec 15 00:00:00 CST 2017, maps={k1=v1, k2=14}, lists=[a, b, c], dog=Dog{name='dog01', age=15}}

三、@ConfigurationProperties注解的特点

1.批量注入配置文件中值

2.支持松散绑定

配置文件中属性格式分别为lastname  、last_name、last-name 时,都可以与Person类中属性lastName绑定

person:
  lastname:  fang \n xin \n de

3.支持JSR303校验:

Person类上加上@Validated,表示类中属性需要进行验证。  @Email表示lastName必须满足邮件格式


@Component
@ConfigurationProperties(prefix = "person")//只能从默认的全局文件中获取
@Validated
public class Person {
    @Email
    private String lastName;
}

配置文件中lastname值不是邮件格式

person:
  lastname:  fang \n xin \n de

输出person对应的实例时报错:

4.能够封装复杂结构的属性:例如:map集合、级联属性

5.不支持SpEL表达式

猜你喜欢

转载自blog.csdn.net/fangxinde/article/details/83622248