springboot 注解说明(@EnableConfigurationProperties)


springboot 注解说明(@EnableConfgurationProperties)

 

应用:带有@ConfigurationProperties注解的类A从配置文件中读取配置信息,@EnableConfigurationProperties将类A注册为容器的bean;

说明:也可以不使用@EnableConfigurationProperties,直接在类A上加注解(@Component等)将其注册为容器的bean

 

 

***********************

相关类与注解

 

@EnableConfigurationProperties:导入EnableConfigurationPropertiesRegistrar类

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({EnableConfigurationPropertiesRegistrar.class})
public @interface EnableConfigurationProperties {
    String VALIDATOR_BEAN_NAME = "configurationPropertiesValidator";

    Class<?>[] value() default {};
}

 

EnableConfigurationPropertiesRegistrar

class EnableConfigurationPropertiesRegistrar implements ImportBeanDefinitionRegistrar {
    EnableConfigurationPropertiesRegistrar() {
    }

    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        registerInfrastructureBeans(registry);
        ConfigurationPropertiesBeanRegistrar beanRegistrar = new ConfigurationPropertiesBeanRegistrar(registry);
                                                            //使用ConfigurationPropertiesBeanRegistrar将类注册为容器的bean
        this.getTypes(metadata).forEach(beanRegistrar::register);  //遍历value指定的类,进行注册                                           
    }

    private Set<Class<?>> getTypes(AnnotationMetadata metadata) {  //获取value指定的类                                      
        return (Set)metadata.getAnnotations().stream(EnableConfigurationProperties.class).flatMap((annotation) -> {
            return Arrays.stream(annotation.getClassArray("value"));
        }).filter((type) -> {
            return Void.TYPE != type;
        }).collect(Collectors.toSet());
    }

    static void registerInfrastructureBeans(BeanDefinitionRegistry registry) {
        ConfigurationPropertiesBindingPostProcessor.register(registry);
        BoundConfigurationProperties.register(registry);
        ConfigurationPropertiesBeanDefinitionValidator.register(registry);
        ConfigurationBeanFactoryMetadata.register(registry);
    }
}

 

 

***********************

示例

 

******************

配置文件

 

application.yml

person:
  name: 瓜田李下
  age: 20

 

******************

pojo 层

 

@Data
@ConfigurationProperties(prefix = "person")
public class Person {

    private String name;
    private Integer age;
}

 

******************

config 层

 

WebConfig:使用@EnableConfigurationProperties注解,将person注册为bean

@Configuration
@EnableConfigurationProperties(Person.class)
public class WebConfig {
}

 

******************

controller 层

 

HelloController

@RestController
public class HelloController {

    @Resource
    private Person person;

    @RequestMapping("/hello")
    public String hello(){
        System.out.println(person);

        return "success";
    }
}

 

 

***********************

使用测试

 

localhost:8080/hello

2020-07-05 16:33:22.845  INFO 14192 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-07-05 16:33:22.852  INFO 14192 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 6 ms
Person(name=瓜田李下, age=20)

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章