一、使用
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;
}
}
- @Component 让 Bean 被扫描
- @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
- 默认 false,表示不忽略无效的字段
- 无效字段,一般是指配置了错误的类型
- 类型是 Boolean,配置却不是 true/false
- 类型是 Integer,配置却不是数字
- 当为 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"
- 当为 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
本文详细介绍了Spring Boot中@ConfigurationProperties注解的使用方法,包括如何配置Bean、添加依赖及运行方式。同时,深入探讨了ignoreInvalidFields和ignoreUnknownFields参数的作用,以及它们在配置过程中的影响。
1230

被折叠的 条评论
为什么被折叠?



