Spring Boot注解全攻略(十二):@EnableConfigurationProperties

658 阅读1分钟

前言

我们从前面大概知道,在Spring中,想让一个类被扫描进入IoC容器中,一般有两种方法:一是在这个类上添加@Component;二是在配置类上指定类。第二类的方法对应到实际中就是大多以Enable开头,例如@EnableConfigurationProperties就是和@ConfigurationProperties搭配使用。

源码

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

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

使用

使用使用时要加载配置类上,启动类也是一种配置类。

@Configuration
@EnableConfigurationProperties(YouConfigurationProperties.class)
public class MyConfig {
    // ...
}