@ConfigurationProperties
@ConfigurationProperties is springboot provides for mapping property values in configuration files to Java bean objects. By using this annotation, we can easily bind the values in the property file to an instantiated class object, making it easy to use these properties in the application.
@ConfigurationProperties annotation has a parameter prefix to specify the property public prefix
@Configuration @ConfigurationProperties(prefix = "myconfig") @Data public class MyConfigProperty { private int port; //The attributes not found will not be injected private String hhh; }
Generally, it is used as an attribute injection object and is first defined as a @Configuration. Then use @ConfigurationProperties to specify the prefix for the associated property.
In this way, if some values in the configuration file are automatically bound to the port property of the MyConfigProperty class. The premise is that there must be a corresponding set method.
In addition to annotating on the class, you can also use the @Bean method
@Configuration public class MyConfigByMethod { @Bean @ConfigurationProperties(prefix = "myconfig") public MyConfig myConfig(){ return new MyConfig(); } }
Observe the source code of spring, and use @EnableConfigurationProperties to introduce beans modified by @ConfigurationProperties.
Automatic framework assembly analysis
In the automatic assembly of springboot framework, there is a built-in configuration class ConfigurationPropertiesAutoConfiguration used to handle @ConfigurationProperties annotation. This configuration class introduces @EnableConfigurationProperties, and then indirectly introduces EnableConfigurationPropertiesRegistrar. When the configuration is initialized, the registerBeanDefinitions() method will be called to load the beanDef in the configuration class.
EnableConfigurationPropertiesRegistrar#registerBeanDefinitions
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { registerInfrastructureBeans(registry); registerMethodValidationExcludeFilter(registry); ConfigurationPropertiesBeanRegistrar beanRegistrar = new ConfigurationPropertiesBeanRegistrar(registry); getTypes(metadata).forEach(beanRegistrar::register); } private Set<Class<?>> getTypes(AnnotationMetadata metadata) { return ().stream() .flatMap((annotation) -> (())) .filter((type) -> != type).collect(()); } static void registerInfrastructureBeans(BeanDefinitionRegistry registry) { (registry); (registry); }
Here we will mainly complete two events:
1. registerInfrastructureBeans will register ConfigurationPropertiesBindingPostProcessor into the container. This is a post-processing process. The assignment of attributes will be completed in its post-processing method.
2. Register the appropriate ConfigurationProperties type bean. The current metadata is the Configuration class that is being initialized, and then obtain the EnableConfigurationProperties annotation from its annotation and load it into the container as a bean definition.
Let’s take a look at some examples of automatic assembly:
ServletWebServerFactoryAutoConfiguration
- If the ServletWebServerFactoryAutoConfiguration has the @EnableConfigurationProperties() annotation, ServerProperties will be processed as a bean.
- ServerProperties is configured with @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true), and the properties of our scene will be injected into the properties.
DataSourceAutoConfiguration
@EnableConfigurationProperties() public class DataSourceAutoConfiguration {} @ConfigurationProperties(prefix = "") public class DataSourceProperties{}
DataSourceProperties will be loaded as a bean, and the properties under "" will be injected into the DataSourceProperties property.
ConfigurationPropertiesBindingPostProcessor
ConfigurationPropertiesBindingPostProcessor is a bean postprocessor, and its post-methods will be called after the bean is instantiated.
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { bind((, bean, beanName)); return bean; }
The () method will determine whether the current bean has ConfigurationProperties annotation, and if so, the corresponding attribute binding will be performed.
Finally, use the class to bind the property.
Here, ConfigurationProperties will have two parts. One is that the framework is automatically assembled through autoConfig, and the other is that we display the beans modified with @ConfigurationProperties ourselves.
Here we see that when we customize @ConfigurationProperties, we may not necessarily use @Configuration for modification. As long as the current class can be parsed into a bean, the post-method will be called for the assignment of corresponding configuration properties.
Attribute metadata information
Configurable properties in each jar package META-INFO/file. In this way, when IDEA configures the application file, it can generally configure prompts based on the metadata information in the file.
For example, configured in a package
{ "name": "", "type": "", "description": "Server HTTP port.", "sourceType": "", "defaultValue": 8080 },
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.