SpringBoot注解@ConfigurationProperties配置提示

本文详细介绍了Spring Boot中@ConfigurationProperties注解的使用方法,包括如何配置Bean、添加依赖及运行方式。同时,深入探讨了ignoreInvalidFields和ignoreUnknownFields参数的作用,以及它们在配置过程中的影响。
一、使用
1. 配置Bean
@Component
@ConfigurationProperties(prefix = "pay")
@Data
public class PayConfig {
	/**
	 * 签名
	 */
	private String signCode = "11111";

	/**
	 * 超时时间,单位:秒
	 */
	private Integer timeOutOfSeconds;

	/**
	 * 请求URL
	 */
	private Url url;

	@Data
	public static class Url {
		/**
		 * url - 微信
		 */
		private String weixin;
		/**
		 * url - 支付宝
		 */
		private String zhifubao;
	}
}
  1. @Component 让 Bean 被扫描
  2. @Data 添加 getter、setter 方法,没有不行
2. 添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <scope>compile</scope>
    <optional>true</optional>
</dependency>
3. run 或 debug 或 maven compile

完成!

第3步后,会生成一个文件:/target/classes/META-INF/spring-configuration-metadata.json
在这里插入图片描述
配置提示效果图
在这里插入图片描述

二、@ConfigurationProperties 相关参数
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {

	@AliasFor("prefix")
	String value() default "";

	@AliasFor("value")
	String prefix() default "";

	boolean ignoreInvalidFields() default false;

	boolean ignoreUnknownFields() default true;

}
1. ignoreInvalidFields
  1. 默认 false,表示不忽略无效的字段
  2. 无效字段,一般是指配置了错误的类型
    1. 类型是 Boolean,配置却不是 true/false
    2. 类型是 Integer,配置却不是数字
  3. 当为 false 时,且配置了无效字段,启动时会报错
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'payConfig': Could not bind properties to 'PayConfig' : prefix=pay, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'pay.time-out-of-seconds' to java.lang.Integer
Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'pay.time-out-of-seconds' to java.lang.Integer
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'asdf'; nested exception is java.lang.NumberFormatException: For input string: "asdf"
Caused by: java.lang.NumberFormatException: For input string: "asdf"
  1. 当为 false 时,且配置了无效字段,启动成功,但不会绑定,字段值为 null
@SpringBootTest
public class ConfigTipTest {

	@Autowired
	private PayConfig payConfig;

	@Test
	public void test01() {
		System.out.println(payConfig.getTimeOutOfSeconds());
	}
}

// 打印结果
null
2. ignoreUnknownFields

忽略未知字段,默认 true

未知字段,就是配置的 key,与 Bean 对应不起来

当 ignoreUnknownFields = false,且配置了未知字段时,启动会报错

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'payConfig': Could not bind properties to 'PayConfig' : prefix=pay, ignoreInvalidFields=false, ignoreUnknownFields=false; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'pay' to com.balsam.study.spring.boot.config.tip.PayConfig
Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'pay' to com.balsam.study.spring.boot.config.tip.PayConfig
Caused by: org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException: The elements [pay.time-out-of-secondsHAHA] were left unbound.

备注:由于配置中的 Key-Value 可能在其它地方被使用,所以 ignoreUnknownFields 参数不建议设置成 false


相关依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
// 包含 spring-boot-autoconfigure
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>2.2.1.RELEASE</version>
    <scope>compile</scope>
</dependency>
// 包含 spring-boot-autoconfigure-processor、spring-boot-configuration-processor
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure-processor</artifactId>
    <version>2.2.1.RELEASE</version>
    <scope>compile</scope>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <version>2.2.1.RELEASE</version>
    <scope>compile</scope>
    <optional>true</optional>
</dependency>
// 需要的是 spring-boot-configuration-processor
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值