Java 如果 bean 已经用 @ConfigurationProperties 注释,@EnableConfigurationproperties 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49880453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 03:10:11  来源:igfitidea点击:

What difference does @EnableConfigurationproperties make if a bean is already annotated with @ConfigurationProperties?

javaspringspring-boot

提问by J Person

The Spring Boot documentationsays that to use the @ConfigurationPropertiesannotation

Spring Boot 文档说要使用@ConfigurationProperties注释

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

您还需要在@EnableConfigurationProperties注解中列出要注册的属性类 ,如以下示例所示:

and gives this code:

并给出以下代码:

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

But in the very next paragraph says:

但在下一段中说:

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

即使前面的配置为 AcmeProperties 创建了一个普通的 bean,我们建议@ConfigurationProperties只处理环境,特别是不要从上下文中注入其他 bean。话虽如此,@EnableConfigurationProperties注释也会自动应用于您的项目,以便从环境中配置任何用 @ConfigurationProperties 注释的现有 bean

Suggesting that listing a @ConfigurationPropertiesbean under an @EnableConfigurationPropertiesannotation is not necessary.

建议@ConfigurationProperties@EnableConfigurationProperties注释下列出bean是不必要的。

So which is it? Experimentally, I've seen that if I annotate a bean with @ConfigurationPropertiesit 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 @ConfigurationPropertiesannotation under @EnableConfigurationProperties, as is shown in the documentation? Does it make any kind of difference?

那么它是哪个?实验上,我已经看到了,如果我注释一个bean与@ConfigurationProperties它得到的属性注入到其预期,而无需列出它@EnableConfigurationProperties,但如果是这样的话,那么为什么列表任何有@ConfigurationProperties下注解@EnableConfigurationProperties,如文档中所示? 它有什么不同吗?

采纳答案by Emanuel Miranda

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

正如 M. Deinum 所提到的,@EnableConfigurationProperties是为了支持@ConfigurationProperties. 如果您查看注释 Java Doc,您可以看到:

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. [...]

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

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

例如,假设您有一个类,其职责是从您的application.yml/读取和存储信息,application.properties这是与不同数据库建立连接所必需的。你用@ConfigurationProperties.

Then, you typically have a @Configurationannotated class that provides a DataSource@Beanto your application. You can use the @EnableConfigurationPropertiesto link it to the @ConfigurationPropertiesclass and init your data sources accordingly.

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

Here is a small example:

这是一个小例子:

application.yml

应用程序.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..
    }

}

回答by AR1

If we look at the code below:

如果我们看下面的代码:

@Configuration @EnableConfigurationProperties @ConfigurationProperties(prefix="ar1") public class ar1Settings { }

@Configuration @EnableConfigurationProperties @ConfigurationProperties(prefix="ar1") public class ar1Settings { }

  • @Configuration?tells Spring to treat this as a configuration class and register it as a Bean

  • @EnableConfigurationProperties?tells Spring to treat this class as a consumer of application.yml/properties values

  • @ConfigurationPropertiestells Spring what section this class represents.

  • @Configuration?告诉 Spring 将其视为配置类并将其注册为 Bean

  • @EnableConfigurationProperties?告诉 Spring 将此类视为 application.yml/properties 值的使用者

  • @ConfigurationProperties告诉 Spring 这个类代表什么部分。

My understanding is that if you don't need to specify the section of the property file, then @ConfigurationProperties can be omitted.

我的理解是,如果不需要指定属性文件的section,那么@ConfigurationProperties可以省略。

回答by alpha

It took me a while to reach to this post but would like to add here so that others may get benefited.

我花了一段时间才看到这篇文章,但我想在这里补充一下,以便其他人可以从中受益。

@ConfigurationProperties- Used to bind a class with an externalized property file. Very powerful and must be used to separate out bean classes with configuration entity class.

@ConfigurationProperties- 用于将类与外部化的属性文件绑定。非常强大,必须用于将 bean 类与配置实体类分开。

@Configuration- Creates a Spring bean of configuration stereotype.

@Configuration- 创建一个配置构造型的 Spring bean。

@EnableConfigurationProperties- Creates a binding between a configuration entity class and Spring configuration stereotype so that after injection within a service properties can be retrieved easily.

@EnableConfigurationProperties- 在配置实体类和 Spring 配置构造型之间创建绑定,以便在注入后可以轻松检索服务属性。