gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:47:25 47 4
gpt4 key购买 nike

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

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

并给出以下代码:

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

但在下一段中说:

Even if the preceding configuration creates a regular bean forAcmeProperties, we recommend that @ConfigurationProperties only dealwith the environment and, in particular, does not inject other beansfrom the context. Having said that, the @EnableConfigurationPropertiesannotation is also automatically applied to your project so that anyexisting bean annotated with @ConfigurationProperties is configuredfrom 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/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com