предисловие
В процессе разработки с помощью springboot мы будем использовать аннотацию @ConfigurationProperties, которая в основном используется для преобразования свойств или файлов конфигурации yml в bean-компоненты, а функция аннотации @EnableConfigurationProperties заключается в том, что аннотация @ConfigurationProperties вступает в силу. Если настроена только аннотация @ConfigurationProperties, bean-компоненты, преобразованные из файла конфигурации свойств, не могут быть получены в контейнере IOC.Конечно, добавление @Component в класс, аннотированный с помощью @ConfigurationProperties, также может управляться springboot.
взять каштан
первый шаг: Создайте класс TestConfigurationProperties.
@ConfigurationProperties(prefix = "properties")
public class TestConfigurationProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Уведомление: нужно добавить методы set и get
второй шаг: Создать класс TestAutoConfiguration.
@Configuration
@EnableConfigurationProperties(TestConfigurationProperties.class)
public class TestAutoConfiguration {
private TestConfigurationProperties testConfigurationProperties;
public TestAutoConfiguration(TestConfigurationProperties testConfigurationProperties) {
this.testConfigurationProperties = testConfigurationProperties;
}
@Bean
public User user(){
User user = new User();
user.setName(testConfigurationProperties.getName());
return user;
}
}
Уведомление: нужно создать параметризованный конструктор
третий шаг: файл конфигурации добавить свойства
properties.name=test
четвертый шаг: Запустите его и распечатайте класс User
@RestController
@RequestMapping("/api/test")
@Slf4j
public class TestController {
@Autowired
TestConfigurationProperties testConfigurationProperties;
@Autowired
User user;
@RequestMapping(value = "/testConfigurationProperties")
public String testConfigurationProperties() {
log.info("test testConfigurationProperties.............{}", testConfigurationProperties.getName());
log.info("user:{}", user);
return "SUCCESS";
}
}
Вывод консоли:
2019-04-21/16:11:36.638||||||||^_^|[http-nio-8088-exec-1] INFO com.stone.zplxjj.controller.TestController 37 - test testConfigurationProperties.............test
2019-04-21/16:11:36.639||||||||^_^|[http-nio-8088-exec-1] INFO com.stone.zplxjj.controller.TestController 38 - user:User(id=null, name=test)
Как загружается @EnableConfigurationProperties?
Глядя на аннотацию @EnableConfigurationProperties:
@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 {};
}
Анализируя автоматическую настройку, мы можем знать, что это должна быть функция этого класса EnableConfigurationPropertiesImportSelector:
private static final String[] IMPORTS = {
ConfigurationPropertiesBeanRegistrar.class.getName(),
ConfigurationPropertiesBindingPostProcessorRegistrar.class.getName() };
@Override
public String[] selectImports(AnnotationMetadata metadata) {
return IMPORTS;
}
Метод selectImports возвращает эти два класса: ConfigurationPropertiesBeanRegistrar и ConfigurationPropertiesBindingPostProcessorRegistrar, когда они загружены, нам нужно только посмотреть на этот класс ConfigurationPropertiesBeanRegistrar:
public static class ConfigurationPropertiesBeanRegistrar
implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) {
getTypes(metadata).forEach((type) -> register(registry,
(ConfigurableListableBeanFactory) registry, type));
}
//找到加入这个注解@EnableConfigurationProperties里面的value值,其实就是类class
private List<Class<?>> getTypes(AnnotationMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata
.getAllAnnotationAttributes(
EnableConfigurationProperties.class.getName(), false);
return collectClasses((attributes != null) ? attributes.get("value")
: Collections.emptyList());
}
private List<Class<?>> collectClasses(List<?> values) {
return values.stream().flatMap((value) -> Arrays.stream((Object[]) value))
.map((o) -> (Class<?>) o).filter((type) -> void.class != type)
.collect(Collectors.toList());
}
//注册方法:根据找到的类名name和type,将加入注解@ConfigurationProperties的类加入spring容器里面
private void register(BeanDefinitionRegistry registry,
ConfigurableListableBeanFactory beanFactory, Class<?> type) {
String name = getName(type);
if (!containsBeanDefinition(beanFactory, name)) {
registerBeanDefinition(registry, name, type);
}
}
//找到加入注解@ConfigurationProperties的类的名称,加入一定格式的拼接
private String getName(Class<?> type) {
ConfigurationProperties annotation = AnnotationUtils.findAnnotation(type,
ConfigurationProperties.class);
String prefix = (annotation != null) ? annotation.prefix() : "";
return (StringUtils.hasText(prefix) ? prefix + "-" + type.getName()
: type.getName());
}
private boolean containsBeanDefinition(
ConfigurableListableBeanFactory beanFactory, String name) {
if (beanFactory.containsBeanDefinition(name)) {
return true;
}
BeanFactory parent = beanFactory.getParentBeanFactory();
if (parent instanceof ConfigurableListableBeanFactory) {
return containsBeanDefinition((ConfigurableListableBeanFactory) parent,
name);
}
return false;
}
private void registerBeanDefinition(BeanDefinitionRegistry registry, String name,
Class<?> type) {
assertHasAnnotation(type);
GenericBeanDefinition definition = new GenericBeanDefinition();
definition.setBeanClass(type);
registry.registerBeanDefinition(name, definition);
}
private void assertHasAnnotation(Class<?> type) {
Assert.notNull(
AnnotationUtils.findAnnotation(type, ConfigurationProperties.class),
() -> "No " + ConfigurationProperties.class.getSimpleName()
+ " annotation found on '" + type.getName() + "'.");
}
}
Эпилог
Кроме того, есть такой класс: ConfigurationPropertiesBindingPostProcessorRegistrar.Я его сейчас не анализировал.Посмотрел исходный код.На самом деле то,что он сделал,это присвоил значения атрибутов в конфигурационном файле атрибутам класса с аннотацией @ConfigurationProperties.Если вам интересно, вы можете прочитать это сами.Если вы знаете вход, это будет просто.
Я также открыл публичный аккаунт в WeChat: stonezplxjj и личный блог:www.zplxjj.com, больше статей можно обратить внимание на общедоступный номер: