@@ -7402,6 +7402,9 @@ jdbc.password=
7402
7402
}
7403
7403
----
7404
7404
7405
+
7406
+
7407
+
7405
7408
[[beans-environment]]
7406
7409
== Environment abstraction
7407
7410
@@ -7423,6 +7426,8 @@ on. The role of the `Environment` object with relation to properties is to provi
7423
7426
user with a convenient service interface for configuring property sources and resolving
7424
7427
properties from them.
7425
7428
7429
+
7430
+
7426
7431
[[beans-definition-profiles]]
7427
7432
=== Bean definition profiles
7428
7433
@@ -7497,7 +7502,7 @@ can rewrite the `dataSource` configuration as follows:
7497
7502
[subs="verbatim,quotes"]
7498
7503
----
7499
7504
@Configuration
7500
- **@Profile("dev ")**
7505
+ **@Profile("development ")**
7501
7506
public class StandaloneDataConfig {
7502
7507
7503
7508
@Bean
@@ -7549,56 +7554,76 @@ of creating a custom _composed annotation_. The following example defines a cust
7549
7554
}
7550
7555
----
7551
7556
7557
+ [TIP]
7558
+ ====
7559
+ If a `@Configuration` class is marked with `@Profile`, all of the `@Bean` methods and
7560
+ `@Import` annotations associated with that class will be bypassed unless one or more of
7561
+ the specified profiles are active. If a `@Component` or `@Configuration` class is marked
7562
+ with `@Profile({"p1", "p2"})`, that class will not be registered/processed unless
7563
+ profiles 'p1' and/or 'p2' have been activated. If a given profile is prefixed with the
7564
+ NOT operator (`!`), the annotated element will be registered if the profile is **not**
7565
+ active. For example, given `@Profile({"p1", "!p2"})`, registration will occur if profile
7566
+ 'p1' is active or if profile 'p2' is not active.
7567
+ ====
7568
+
7552
7569
`@Profile` can also be declared at the method level to include only one particular bean
7553
- of a configuration class:
7570
+ of a configuration class, e.g. for alternative variants of a particular bean :
7554
7571
7555
7572
[source,java,indent=0]
7556
7573
[subs="verbatim,quotes"]
7557
7574
----
7558
7575
@Configuration
7559
7576
public class AppConfig {
7560
7577
7561
- @Bean
7562
- **@Profile("dev ")**
7563
- public DataSource devDataSource () {
7578
+ @Bean("dataSource")
7579
+ **@Profile("development ")**
7580
+ public DataSource standaloneDataSource () {
7564
7581
return new EmbeddedDatabaseBuilder()
7565
7582
.setType(EmbeddedDatabaseType.HSQL)
7566
7583
.addScript("classpath:com/bank/config/sql/schema.sql")
7567
7584
.addScript("classpath:com/bank/config/sql/test-data.sql")
7568
7585
.build();
7569
7586
}
7570
7587
7571
- @Bean
7588
+ @Bean("dataSource")
7572
7589
**@Profile("production")**
7573
- public DataSource productionDataSource () throws Exception {
7590
+ public DataSource jndiDataSource () throws Exception {
7574
7591
Context ctx = new InitialContext();
7575
7592
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
7576
7593
}
7577
7594
}
7578
7595
----
7579
7596
7580
- [TIP ]
7597
+ [NOTE ]
7581
7598
====
7582
- If a `@Configuration` class is marked with `@Profile`, all of the `@Bean` methods and
7583
- `@Import` annotations associated with that class will be bypassed unless one or more of
7584
- the specified profiles are active. If a `@Component` or `@Configuration` class is marked
7585
- with `@Profile({"p1", "p2"})`, that class will not be registered/processed unless
7586
- profiles 'p1' and/or 'p2' have been activated. If a given profile is prefixed with the
7587
- NOT operator (`!`), the annotated element will be registered if the profile is **not**
7588
- active. For example, given `@Profile({"p1", "!p2"})`, registration will occur if profile
7589
- 'p1' is active or if profile 'p2' is not active.
7599
+ With `@Profile` on `@Bean` methods, a special scenario may apply: In the case of
7600
+ overloaded `@Bean` methods of the same Java method name (analogous to constructor
7601
+ overloading), an `@Profile` condition needs to be consistently declared on all
7602
+ overloaded methods. If the conditions are inconsistent, only the condition on the
7603
+ first declaration among the overloaded methods will matter. `@Profile` can therefore
7604
+ not be used to select an overloaded method with a particular argument signature over
7605
+ another; resolution between all factory methods for the same bean follows Spring's
7606
+ constructor resolution algorithm at creation time.
7607
+
7608
+ If you would like to define alternative beans with different profile conditions,
7609
+ use distinct Java method names pointing to the same bean name via the `@Bean` name
7610
+ attribute, as indicated in the example above. If the argument signatures are all
7611
+ the same (e.g. all of the variants have no-arg factory methods), this is the only
7612
+ way to represent such an arrangement in a valid Java class in the first place
7613
+ (since there can only be one method of a particular name and argument signature).
7590
7614
====
7591
7615
7616
+
7592
7617
[[beans-definition-profiles-xml]]
7593
- === XML bean definition profiles
7618
+ ==== XML bean definition profiles
7594
7619
7595
7620
The XML counterpart is the `profile` attribute of the `<beans>` element. Our sample
7596
7621
configuration above can be rewritten in two XML files as follows:
7597
7622
7598
7623
[source,xml,indent=0]
7599
7624
[subs="verbatim,quotes"]
7600
7625
----
7601
- <beans profile="dev "
7626
+ <beans profile="development "
7602
7627
xmlns="http://www.springframework.org/schema/beans"
7603
7628
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7604
7629
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
7637
7662
7638
7663
<!-- other bean definitions -->
7639
7664
7640
- <beans profile="dev ">
7665
+ <beans profile="development ">
7641
7666
<jdbc:embedded-database id="dataSource">
7642
7667
<jdbc:script location="classpath:com/bank/config/sql/schema.sql"/>
7643
7668
<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
7671
7696
[subs="verbatim,quotes"]
7672
7697
----
7673
7698
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
7674
- ctx.getEnvironment().setActiveProfiles("dev ");
7699
+ ctx.getEnvironment().setActiveProfiles("development ");
7675
7700
ctx.register(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
7676
7701
ctx.refresh();
7677
7702
----
0 commit comments