SpringBoot中的@ConfigurationProperties

有什么用

该注解的作用类似于@Value,即为组件中的属性进行赋值。

怎么用

首先是建立一个springboot的工程,不再赘述。

首先我们建立一个Person类和一个Dog类。

package com.helius.springboot.bean;

@Component //加上它,即成为ioc容器中的一个组件
@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;
    // 省略了setter、getter、toString()等
}

再来看SpringBoot的主配置文件application.properties

person.last-name=张三
person.age=18
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=perter
person.dog.age=15

在springboot的测试类中进行测试

/**
 * SpringBoot单元测试;
 *
 * 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {

    @Autowired
    Person person;

    @Test
    public void contextLoads() {
        System.out.println(person);
    }

}

控制台打印一下结果

Person{lastName='张三', age=18, boss=false, birth=Fri Dec 15 00:00:00 CST 2017, maps={k1=v1, k2=14}, lists=[a, b, c], dog=Dog{name='perter', age=15}}

我们发现,容器中person组件已经全部被赋值了。


解释

因为我们使用了@ConfigurationProperties(prefix = "person")这个注解

将配置文件中配置的每一个属性的值,映射到这个组件中

==@ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定; prefix = "person":配置文件中哪个下面的所有属性进行一一映射==

相关注解

在之前的博文中:Spring中使用@Value和@PropertySource为属性赋值中介绍@Value注解,

它能够为组件注入外部配置文件中的值。

那在springboot中的,可以直接使用@Value注解,不需要导入主配置文件application.properties,毕竟它是主配置文件,也是放在springboot约定的位置的。

既然@Value和@ConfigurationProperties都能为组件赋值,那区别呢?

@ConfigurationProperties@Value
功能批量注入配置文件中的属性一个个指定
松散语法支持不支持
SpEL不支持支持
JSR303数据校验支持不支持
复杂类型封装支持不支持
  1. 松散语法:可以发现配置文件中我们使用person.last-name也能够为Person类中的lastName属性进行赋值。

其实你使用last-name还是last_name亦或lastName都能够为Pseron中对应的属性赋值,这叫松散绑定

  1. @Value支持Spel这个可以在之前的博文中看到 ,而 @ConfigurationProperties 是不支持的。
  2. 我们在springmvc中使用过@Validated这个注解。在@ConfigurationProperties也是支持的,使用示例放在最后面
  3. 最后一点,@Value不能够对复杂类型进行赋值,如private Map<String,Object> maps;该属性,就无法在属性加上

@Vavlue(${person.map})是拿不到值的。


@Validated应用举例

改造一下Person类。

package com.helius.springboot.bean;

@Component //加上它,即成为ioc容器中的一个组件
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
    @Email
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
    // 省略了setter、getter、toString()等
}

这是在赋值的时候检查lastName必须是邮件格式,

重新运行测试方法

Field error in object 'person' on field 'lastName': rejected value [张三]; codes [Email.person.lastName,Email.lastName,Email.java.lang.String,Email]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [person.lastName,lastName]; arguments []; default message [lastName],[Ljavax.validation.constraints.Pattern$Flag;@702ed190,org.springframework.validation.beanvalidation.SpringValidatorAdapter$ResolvableAttribute@173b9122]; default message [不是一个合法的电子邮件地址]

转载于:https://www.cnblogs.com/heliusKing/p/11487360.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值