Spring Boot 中@ConfigurationProperties注解使用

@ConfigurationProperties注解用于自动配置绑定,可以将application.properties配置中的值注入到bean对象上。

  • 该注解使用必须将对象注入到IOC容器中才有配置绑定的功能。

  • 该注解可以使用在类上、方法上;会自动进行配置导入。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface ConfigurationProperties {
	//1. 匹配的前缀
    @AliasFor("prefix")
    String value() default "";

	//2. 同上
    @AliasFor("value")
    String prefix() default "";

	//3. 忽略属性类型不匹配的字段
    boolean ignoreInvalidFields() default false;

	//4. 忽略类中未知的属性
    boolean ignoreUnknownFields() default true;
}

一、基本使用

先在application.properties中配置所需的参数

编写一个User类、Car类分别使用@ConfigurationProperties在类上配置和方法上配置。

测试@ConfigurationProperties各属性如何使用。

application.properties中配置如下:

user.name=张三
user.age=25

car.color=蓝色

1.1 两种基本使用

标注在组件类上使用,一定要有组件注解!

@Data                       //get/set方法
@AllArgsConstructor         //全参构造
@NoArgsConstructor          //无参构造
@Component("user")           //组件注册, 必须有组件注册手段
@ConfigurationProperties(prefix = "user")
public class User {
    private String name;
    private Integer age;
}

或 标注在方法上使用

@Configuration
public class MyConfig {

    @Bean("user")
    @ConfigurationProperties(prefix = "user")
    public User user(){
        return new User();
    }
}

1.2 字段不匹配

  • ignoreInvalidFields默认情况为true, 会自动忽略值类型不匹配的字段

  • 当ignoreInvalidFields=false手动关闭时, 如果值类型不匹配将会爆出异常

@Data                       //get/set方法
@AllArgsConstructor         //全参构造
@NoArgsConstructor          //无参构造
@Component("user")           //组件注册, 必须有组件注册手段
@ConfigurationProperties(prefix = "user", ignoreInvalidFields = false)
public class User {

    private int age;
    private String name;
}

1.3 未知属性字段

  • 默认情况下ignoreUnknownFields属性是true,会忽略掉对象中未知的字段。

  • 手动关闭ignoreUnknownFields=false后, 当出现未知字段时会出现异常。

@Data                       //get/set方法
@AllArgsConstructor         //全参构造
@NoArgsConstructor          //无参构造
@Component("user")           //组件注册, 必须有组件注册手段
@ConfigurationProperties(prefix = "user", ignoreUnknownFields = true)
public class User {

    private String name;			
    private Integer age;
    private String origin;			//忽略
}

二、@EnableConfigurationProperties注解

可以看到上面的配置绑定需要使用@Component组件注解进行注册才能进行绑定,如果是写好的第三方包呢?那么没有办法给第三方包加入的时候可以使用使用方法标记进行配置绑定或者@EnableConfigurationProperties注解进行自动注册

@Data                       //get/set方法
@AllArgsConstructor         //全参构造
@NoArgsConstructor          //无参构造
@ConfigurationProperties(prefix = "user")
public class User {

    private String name;
    private Integer age;

}
@Configuration
@EnableConfigurationProperties(value = {User.class})		//自动配置绑定
public class MyConfig {

	@Bean("user")
    @ConfigurationProperties(prefix = "user")
    public User user(){
        return new User();
    }

}

补充

  • 根据目前所测试的只有绑定的属性字段与配置中所配置的属性名称一致(忽略大小写)才会进行自动的配置绑定

  • 字段名不匹配就不会进行自动配置

  • @Data                       //get/set方法
    @AllArgsConstructor         //全参构造
    @NoArgsConstructor          //无参构造
    @Component("user")           //组件注册, 必须有组件注册手段
    @ConfigurationProperties(prefix = "user")
    public class User {
        private String name;
        private Integer age;
        private String address;
        private String email;
    
    }
    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

time Friend

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值