@EnableAutoConfiguration、@ConfigurationProperties

@EnableConfigurationProperties 注解的使用
SpringBoot之@EnableAutoConfiguration原理及自定义扩展

@EnableAutoConfiguration可以自动载入应用程序所需的所有Bean——这依赖于Spring Boot在类路径中的查找

可以配合@ConfigurationProperties使用

@ConfigurationProperties注解,主要是用来把properties或者yml配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用使@ConfigurationProperties注解生效,如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的

说白了 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入

例子:

@Configuration
@EnableConfigurationProperties({
     MockProperties.class})
@ConditionalOnProperty(prefix = "hanyinh.mock", name = "isMock", havingValue = "true", matchIfMissing = true)
public class MockAutoConfiguration {
     

    @Resource
    MockProperties properties;

    @Bean
    public RpcClientMockAspect getRpcClientMockAspect() {
     
        RpcClientMockAspect aspect = new RpcClientMockAspect();
        MockServerConfig serverConfig = new MockServerConfig();
        serverConfig.setIsMock(properties.getIsMock());
        serverConfig.setServer(properties.getServer());
        serverConfig.setMockUser(properties.getUser());
        aspect.setMockServerConfig(serverConfig);
        return aspect;
    }
}

// mmc.rpc.mock这是配置文件的前缀
@ConfigurationProperties("mmc.rpc.mock")
@Data
public class MockProperties {
     
    private Boolean isMock;
    private String server;
    private String version;
    private String user;
}

点击进入@EnableConfigurationProperties,里面有个@Import(EnableConfigurationPropertiesImportSelector.class)
EnableAutoConfigurationImportSelector类使用了Spring Core包的SpringFactoriesLoader类的loadFactoryNamesof()方法。
SpringFactoriesLoader会查询META-INF/spring.factories文件中包含的JAR文件。
当找到spring.factories文件后,SpringFactoriesLoader将查询配置文件命名的属性进行自动配置

spring.factories文件


# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration

你可能感兴趣的:(@EnableAutoConfiguration、@ConfigurationProperties)