# 精尽 Spring Boot 源码分析 —— @ConfigurationProperties # 1. 概述 本文我们来分享 `@ConfigurationProperties` 注解,如何将配置文件自动设置到被注解的类。代码如下: ``` // ConfigurationProperties.java /** * Annotation for externalized configuration. Add this to a class definition or a * {@code @Bean} method in a {@code @Configuration} class if you want to bind and validate * some external Properties (e.g. from a .properties file). *
* Note that contrary to {@code @Value}, SpEL expressions are not evaluated since property
* values are externalized.
*
* @author Dave Syer
* @see ConfigurationPropertiesBindingPostProcessor
* @see EnableConfigurationProperties
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
/**
* The name prefix of the properties that are valid to bind to this object. Synonym
* for {@link #prefix()}. A valid prefix is defined by one or more words separated
* with dots (e.g. {@code "acme.system.feature"}).
*
* @return the name prefix of the properties to bind
*/
@AliasFor("prefix")
String value() default "";
/**
* The name prefix of the properties that are valid to bind to this object. Synonym
* for {@link #value()}. A valid prefix is defined by one or more words separated with
* dots (e.g. {@code "acme.system.feature"}).
*
* @return the name prefix of the properties to bind
*/
@AliasFor("value")
String prefix() default "";
/**
* Flag to indicate that when binding to this object invalid fields should be ignored.
* Invalid means invalid according to the binder that is used, and usually this means
* fields of the wrong type (or that cannot be coerced into the correct type).
*
* @return the flag value (default false)
*/
boolean ignoreInvalidFields() default false;
/**
* Flag to indicate that when binding to this object unknown fields should be ignored.
* An unknown field could be a sign of a mistake in the Properties.
*
* @return the flag value (default true)
*/
boolean ignoreUnknownFields() default true;
}
```
`@ConfigurationProperties` 注解有两种使用方法,可见 [《关与 @EnableConfigurationProperties 注解》](https://www.jianshu.com/p/7f54da1cb2eb) 文章。总结来说:
- 第一种,`@Component` + `@ConfigurationProperties` 。
- 第二种,`@EnableConfigurationProperties` + `ConfigurationProperties` 。
实际情况下,更多的是使用第一种。当然,第二种的 `@EnableConfigurationProperties` 的效果,也是将指定的类,实现和 `@Component` 被注解的类是一样的,创建成 Bean 对象。
这样,`@ConfigurationProperties` 就可以将配置文件自动设置到该 Bean 对象咧。
# 2. @EnableConfigurationProperties
`org.springframework.boot.context.properties.@EnableConfigurationProperties` 注解,可以将指定带有 `@ConfigurationProperties` 的类,注册成 BeanDefinition ,从而创建成 Bean 对象。代码如下:
```
// EnableConfigurationProperties.java
/**
* Enable support for {@link ConfigurationProperties} annotated beans.
* {@link ConfigurationProperties} beans can be registered in the standard way (for
* example using {@link Bean @Bean} methods) or, for convenience, can be specified
* directly on this annotation.
*
* @author Dave Syer
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EnableConfigurationPropertiesImportSelector.class)
public @interface EnableConfigurationProperties {
/**
* 指定的类们
*
* Convenient way to quickly register {@link ConfigurationProperties} annotated beans
* with Spring. Standard Spring Beans will also be scanned regardless of this value.
* @return {@link ConfigurationProperties} annotated beans to register
*/
Class>[] value() default {};
}
```
- 从 `@Import` 注解上,可以看到使用 EnableConfigurationPropertiesImportSelector 处理。详细的解析,见 [「2.2 EnableConfigurationPropertiesImportSelector」](https://svip.iocoder.cn/Spring-Boot/ConfigurationProperties/#) 。
## 2.1 ConfigurationPropertiesAutoConfiguration
默认情况下,`@EnableConfigurationProperties` 会通过 `org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration` 类,进行开启。代码如下:
```
// ConfigurationPropertiesAutoConfiguration.java
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link ConfigurationProperties}
* beans. Automatically binds and validates any bean annotated with
* {@code @ConfigurationProperties}.
*
* @author Stephane Nicoll
* @since 1.3.0
* @see EnableConfigurationProperties
* @see ConfigurationProperties
*/
@Configuration
@EnableConfigurationProperties //