问题描述
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有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!