java - 如果 bean 已经用 @ConfigurationProperties 注释,@EnableConfigurationProperties 有什么区别?

标签 java spring spring-boot

The Spring Boot documentation说要使用 @ConfigurationProperties 注释

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 {
}

但在下一段中说:

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。

那是什么?实验上,我已经看到,如果我用 @ConfigurationProperties 注释一个 bean,它会按预期将属性注入(inject)它,而无需在 @EnableConfigurationProperties 中列出它,但如果这是那么为什么要在 @EnableConfigurationProperties 下列出任何具有 @EnableConfigurationProperties 注释的内容,如文档中所示?有什么不同吗?

最佳答案

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

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

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

这是一个小例子:

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

数据源配置

@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;
    }
}

数据源连接配置

@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..
    }

}

关于java - 如果 bean 已经用 @ConfigurationProperties 注释,@EnableConfigurationProperties 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49880453/

相关文章:

java - 返回枚举名称和字符串值作为 api 响应

java - 找不到变量 e.getkeycode

java - 访问 Spring 3.1 应用程序 xml 中的 PropertySource

java - spring-boot mvc 测试 : how to disable validation?

java - Spring MVC PUT 请求返回 405 方法不允许

java - jdbcTemplate 中的 ResultSetExtractor 抛出编译错误

Java 包与其他包位于单独的存储库中

java - 进行延迟加载的更简洁的方法

java - 多个if条件,如何优化

java - Spring Security ACL(访问控制列表)如何处理每个对象标识具有多个权限的主体?