nableConfigurationproperties有什么区

nableConfigurationproperties有什么区

本文介绍了如果已经使用@ConfigurationProperties注释了bean,则@EnableConfigurationproperties有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Boot文档表示要使用@ConfigurationProperties批注

并给出以下代码:

@Configuration
@EnableConfigurationProperties(AcmeProperties.class)
public class MyConfiguration {
}

但是在下一段中说:

建议不必在@EnableConfigurationProperties注释下列出@ConfigurationProperties bean.

Suggesting that listing a @ConfigurationProperties bean under an @EnableConfigurationProperties annotation is not necessary.

那是什么?从实验上看,我已经看到,如果我用@ConfigurationProperties注释一个bean,它将按预期注入属性,而无需在@EnableConfigurationProperties中列出它,但是如果是这种情况,那么为什么要列出任何具有@ConfigurationProperties的东西如文档中所示,在@EnableConfigurationProperties下的注释?有什么区别吗?

So which is it? Experimentally, I've seen that if I annotate a bean with @ConfigurationProperties it gets properties injected to it as expected without needing to list it in @EnableConfigurationProperties, but if this is the case then why list anything that has a @ConfigurationProperties annotation under @EnableConfigurationProperties, as is shown in the documentation? Does it make any kind of difference?

推荐答案

正如M. Deinum提到的,@EnableConfigurationProperties用于启用对@ConfigurationProperties的支持.如果查看批注Java Doc,您将看到:

As M. Deinum referred @EnableConfigurationProperties Is for enabling support of @ConfigurationProperties. If you take a look to the annotation Java Doc you can see:

例如,假设您有一个类,该类的职责是从application.yml/application.properties中读取和存储与不同数据库建立连接所需的信息.您用@ConfigurationProperties对其进行注释.

For example, let's say you have a class whose responsibility is to read and store information from your application.yml / application.properties that is required to make a connection to different databases. You annotate it with @ConfigurationProperties.

然后,通常,您会得到一个带有@Configuration注释的类,该类为您的应用程序提供了一个DataSource @Bean.您可以使用@EnableConfigurationProperties将其链接到@ConfigurationProperties类,并相应地初始化数据源.

Then, you typically have a @Configuration annotated class that provides a DataSource @Bean to your application. You can use the @EnableConfigurationProperties to link it to the @ConfigurationProperties class and init your data sources accordingly.

这是一个小例子:

application.yml

data-sources:
  db1:
    url: "jdbc:postgresql://localhost:5432}/db1"
    username: test
    password: test
  db2:
    url: "jdbc:postgresql://localhost:5432}/db2"
    username: test
    password: test

DataSourcesConfiguration

@ConfigurationProperties
public class DataSourcesConfiguration {

    private Map<String, BasicDataSource> dataSources;

    public void setDataSources(Map<String, BasicDataSource> dataSources) {
        this.dataSources = dataSources;
    }

    Map<String, BasicDataSource > getDataSources() {
        return dataSources;
    }
}

DataSourceConnectionConfiguration

@Configuration
@EnableConfigurationProperties(DataSourcesConfiguration.class)
public class DatabaseConnectionConfiguration implements Provider<Connection> {

    private DataSourcesConfiguration dataSourcesConfiguration;

    public DatabaseConnectionConfiguration(DataSourcesConfiguration dataSourcesConfiguration) {
        this.dataSourcesConfiguration = dataSourcesConfiguration;
    }

    @Bean
    public DataSource dataSource() {
        // Use dataSourcesConfiguration to create application data source. E.g., a AbstractRoutingDataSource..
    }

}

这篇关于如果已经使用@ConfigurationProperties注释了bean,则@EnableConfigurationproperties有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:39