Recommendation for consistent @Profile declarations on overloaded @Bean methods
Issue: SPR-15266
(cherry picked from commit 5d3249f)
This commit is contained in:
@@ -7402,6 +7402,9 @@ jdbc.password=
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
|
||||
|
||||
[[beans-environment]]
|
||||
== Environment abstraction
|
||||
|
||||
@@ -7423,6 +7426,8 @@ on. The role of the `Environment` object with relation to properties is to provi
|
||||
user with a convenient service interface for configuring property sources and resolving
|
||||
properties from them.
|
||||
|
||||
|
||||
|
||||
[[beans-definition-profiles]]
|
||||
=== Bean definition profiles
|
||||
|
||||
@@ -7497,7 +7502,7 @@ can rewrite the `dataSource` configuration as follows:
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
@Configuration
|
||||
**@Profile("dev")**
|
||||
**@Profile("development")**
|
||||
public class StandaloneDataConfig {
|
||||
|
||||
@Bean
|
||||
@@ -7549,34 +7554,6 @@ of creating a custom _composed annotation_. The following example defines a cust
|
||||
}
|
||||
----
|
||||
|
||||
`@Profile` can also be declared at the method level to include only one particular bean
|
||||
of a configuration class:
|
||||
|
||||
[source,java,indent=0]
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
@Configuration
|
||||
public class AppConfig {
|
||||
|
||||
@Bean
|
||||
**@Profile("dev")**
|
||||
public DataSource devDataSource() {
|
||||
return new EmbeddedDatabaseBuilder()
|
||||
.setType(EmbeddedDatabaseType.HSQL)
|
||||
.addScript("classpath:com/bank/config/sql/schema.sql")
|
||||
.addScript("classpath:com/bank/config/sql/test-data.sql")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
**@Profile("production")**
|
||||
public DataSource productionDataSource() throws Exception {
|
||||
Context ctx = new InitialContext();
|
||||
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
[TIP]
|
||||
====
|
||||
If a `@Configuration` class is marked with `@Profile`, all of the `@Bean` methods and
|
||||
@@ -7589,8 +7566,56 @@ active. For example, given `@Profile({"p1", "!p2"})`, registration will occur if
|
||||
'p1' is active or if profile 'p2' is not active.
|
||||
====
|
||||
|
||||
`@Profile` can also be declared at the method level to include only one particular bean
|
||||
of a configuration class, e.g. for alternative variants of a particular bean:
|
||||
|
||||
[source,java,indent=0]
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
@Configuration
|
||||
public class AppConfig {
|
||||
|
||||
@Bean("dataSource")
|
||||
**@Profile("development")**
|
||||
public DataSource standaloneDataSource() {
|
||||
return new EmbeddedDatabaseBuilder()
|
||||
.setType(EmbeddedDatabaseType.HSQL)
|
||||
.addScript("classpath:com/bank/config/sql/schema.sql")
|
||||
.addScript("classpath:com/bank/config/sql/test-data.sql")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean("dataSource")
|
||||
**@Profile("production")**
|
||||
public DataSource jndiDataSource() throws Exception {
|
||||
Context ctx = new InitialContext();
|
||||
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
With `@Profile` on `@Bean` methods, a special scenario may apply: In the case of
|
||||
overloaded `@Bean` methods of the same Java method name (analogous to constructor
|
||||
overloading), an `@Profile` condition needs to be consistently declared on all
|
||||
overloaded methods. If the conditions are inconsistent, only the condition on the
|
||||
first declaration among the overloaded methods will matter. `@Profile` can therefore
|
||||
not be used to select an overloaded method with a particular argument signature over
|
||||
another; resolution between all factory methods for the same bean follows Spring's
|
||||
constructor resolution algorithm at creation time.
|
||||
|
||||
If you would like to define alternative beans with different profile conditions,
|
||||
use distinct Java method names pointing to the same bean name via the `@Bean` name
|
||||
attribute, as indicated in the example above. If the argument signatures are all
|
||||
the same (e.g. all of the variants have no-arg factory methods), this is the only
|
||||
way to represent such an arrangement in a valid Java class in the first place
|
||||
(since there can only be one method of a particular name and argument signature).
|
||||
====
|
||||
|
||||
|
||||
[[beans-definition-profiles-xml]]
|
||||
=== XML bean definition profiles
|
||||
==== XML bean definition profiles
|
||||
|
||||
The XML counterpart is the `profile` attribute of the `<beans>` element. Our sample
|
||||
configuration above can be rewritten in two XML files as follows:
|
||||
@@ -7598,7 +7623,7 @@ configuration above can be rewritten in two XML files as follows:
|
||||
[source,xml,indent=0]
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
<beans profile="dev"
|
||||
<beans profile="development"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
@@ -7637,7 +7662,7 @@ It is also possible to avoid that split and nest `<beans/>` elements within the
|
||||
|
||||
<!-- other bean definitions -->
|
||||
|
||||
<beans profile="dev">
|
||||
<beans profile="development">
|
||||
<jdbc:embedded-database id="dataSource">
|
||||
<jdbc:script location="classpath:com/bank/config/sql/schema.sql"/>
|
||||
<jdbc:script location="classpath:com/bank/config/sql/test-data.sql"/>
|
||||
@@ -7671,7 +7696,7 @@ it programmatically against the `Environment` API which is available via an
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.getEnvironment().setActiveProfiles("dev");
|
||||
ctx.getEnvironment().setActiveProfiles("development");
|
||||
ctx.register(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
|
||||
ctx.refresh();
|
||||
----
|
||||
|
||||
Reference in New Issue
Block a user