如果已经使用@ConfigurationProperties注释了bean,则@EnableConfigurationproperties有什么区别? [英] What difference does @EnableConfigurationproperties make if a bean is already annotated with @ConfigurationProperties?

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

问题描述

您还需要列出要在 @EnableConfigurationProperties批注,如下所示 例如:

You also need to list the properties classes to register in the @EnableConfigurationProperties annotation, as shown in the following example:

并给出以下代码:

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

但是在下一段中说:

即使前面的配置为创建了一个常规bean AcmeProperties,我们建议仅@ConfigurationProperties处理 与环境有关,尤其是不会注入其他豆类 从上下文.话虽如此,@EnableConfigurationProperties 注释也将自动应用于您的项目,以便任何 使用@ConfigurationProperties注释的现有bean已配置 来自环境

Even if the preceding configuration creates a regular bean for AcmeProperties, we recommend that @ConfigurationProperties only deal with the environment and, in particular, does not inject other beans from the context. Having said that, the @EnableConfigurationProperties annotation is also automatically applied to your project so that any existing bean annotated with @ConfigurationProperties is configured from the Environment

建议不必在@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:

启用对带有ConfigurationProperty注释的Bean的支持.可以以标准方式(例如,使用Bean @Bean方法)注册ConfigurationProperties Bean,或者为方便起见,可以在此批注上直接指定ConfigurationProperties Bean. [...]

Enable support for ConfigurationProperties annotated beans. ConfigurationProperties beans can be registered in the standard way (for example using Bean @Bean methods) or, for convenience, can be specified directly on this annotation. [...]

例如,假设您有一个类,该类的职责是从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有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆