@EnableConfigurationProperties、@ConfigurationProperties

Hanyinh發表於2021-01-04

@EnableConfigurationProperties 註解的使用

@EnableConfigurationProperties可以配合@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