@@ -7516,7 +7516,7 @@ can rewrite the `dataSource` configuration as follows:
7516
7516
[subs="verbatim,quotes"]
7517
7517
----
7518
7518
@Configuration
7519
- **@Profile("dev ")**
7519
+ **@Profile("development ")**
7520
7520
public class StandaloneDataConfig {
7521
7521
7522
7522
@Bean
@@ -7581,27 +7581,27 @@ active. For example, given `@Profile({"p1", "!p2"})`, registration will occur if
7581
7581
====
7582
7582
7583
7583
`@Profile` can also be declared at the method level to include only one particular bean
7584
- of a configuration class:
7584
+ of a configuration class, e.g. for alternative variants of a particular bean :
7585
7585
7586
7586
[source,java,indent=0]
7587
7587
[subs="verbatim,quotes"]
7588
7588
----
7589
7589
@Configuration
7590
7590
public class AppConfig {
7591
7591
7592
- @Bean
7593
- **@Profile("dev ")**
7594
- public DataSource devDataSource () {
7592
+ @Bean("dataSource")
7593
+ **@Profile("development ")**
7594
+ public DataSource standaloneDataSource () {
7595
7595
return new EmbeddedDatabaseBuilder()
7596
7596
.setType(EmbeddedDatabaseType.HSQL)
7597
7597
.addScript("classpath:com/bank/config/sql/schema.sql")
7598
7598
.addScript("classpath:com/bank/config/sql/test-data.sql")
7599
7599
.build();
7600
7600
}
7601
7601
7602
- @Bean
7602
+ @Bean("dataSource")
7603
7603
**@Profile("production")**
7604
- public DataSource productionDataSource () throws Exception {
7604
+ public DataSource jndiDataSource () throws Exception {
7605
7605
Context ctx = new InitialContext();
7606
7606
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
7607
7607
}
@@ -7611,11 +7611,20 @@ of a configuration class:
7611
7611
[NOTE]
7612
7612
====
7613
7613
With `@Profile` on `@Bean` methods, a special scenario may apply: In the case of
7614
- overloaded `@Bean` methods, all `@Profile` declarations from all applicable factory
7615
- methods for the same bean will be merged; as a consequence, they all need to match
7616
- for the bean to become registered. `@Profile` can therefore not be used to select
7617
- a particular overloaded method over another; resolution between overloaded factory
7618
- methods only follows Spring's constructor resolution algorithm.
7614
+ overloaded `@Bean` methods of the same Java method name (analogous to constructor
7615
+ overloading), an `@Profile` condition needs to be consistently declared on all
7616
+ overloaded methods. If the conditions are inconsistent, only the condition on the
7617
+ first declaration among the overloaded methods will matter. `@Profile` can therefore
7618
+ not be used to select an overloaded method with a particular argument signature over
7619
+ another; resolution between all factory methods for the same bean follows Spring's
7620
+ constructor resolution algorithm at creation time.
7621
+
7622
+ If you would like to define alternative beans with different profile conditions,
7623
+ use distinct Java method names pointing to the same bean name via the `@Bean` name
7624
+ attribute, as indicated in the example above. If the argument signatures are all
7625
+ the same (e.g. all of the variants have no-arg factory methods), this is the only
7626
+ way to represent such an arrangement in a valid Java class in the first place
7627
+ (since there can only be one method of a particular name and argument signature).
7619
7628
====
7620
7629
7621
7630
@@ -7628,7 +7637,7 @@ configuration above can be rewritten in two XML files as follows:
7628
7637
[source,xml,indent=0]
7629
7638
[subs="verbatim,quotes"]
7630
7639
----
7631
- <beans profile="dev "
7640
+ <beans profile="development "
7632
7641
xmlns="http://www.springframework.org/schema/beans"
7633
7642
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7634
7643
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
@@ -7667,7 +7676,7 @@ It is also possible to avoid that split and nest `<beans/>` elements within the
7667
7676
7668
7677
<!-- other bean definitions -->
7669
7678
7670
- <beans profile="dev ">
7679
+ <beans profile="development ">
7671
7680
<jdbc:embedded-database id="dataSource">
7672
7681
<jdbc:script location="classpath:com/bank/config/sql/schema.sql"/>
7673
7682
<jdbc:script location="classpath:com/bank/config/sql/test-data.sql"/>
@@ -7701,7 +7710,7 @@ it programmatically against the `Environment` API which is available via an
7701
7710
[subs="verbatim,quotes"]
7702
7711
----
7703
7712
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
7704
- ctx.getEnvironment().setActiveProfiles("dev ");
7713
+ ctx.getEnvironment().setActiveProfiles("development ");
7705
7714
ctx.register(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
7706
7715
ctx.refresh();
7707
7716
----
0 commit comments