@ -7423,6 +7426,8 @@ on. The role of the `Environment` object with relation to properties is to provi
@@ -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:
@@ -7497,7 +7502,7 @@ can rewrite the `dataSource` configuration as follows:
[subs="verbatim,quotes"]
----
@Configuration
**@Profile("dev")**
**@Profile("development")**
public class StandaloneDataConfig {
@Bean
@ -7549,8 +7554,20 @@ of creating a custom _composed annotation_. The following example defines a cust
@@ -7549,8 +7554,20 @@ of creating a custom _composed annotation_. The following example defines a cust
}
----
[TIP]
====
If a `@Configuration` class is marked with `@Profile`, all of the `@Bean` methods and
`@Import` annotations associated with that class will be bypassed unless one or more of
the specified profiles are active. If a `@Component` or `@Configuration` class is marked
with `@Profile({"p1", "p2"})`, that class will not be registered/processed unless
profiles 'p1' and/or 'p2' have been activated. If a given profile is prefixed with the
NOT operator (`!`), the annotated element will be registered if the profile is **not**
active. For example, given `@Profile({"p1", "!p2"})`, registration will occur if profile
'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:
of a configuration class, e.g. for alternative variants of a particular bean:
[source,java,indent=0]
[subs="verbatim,quotes"]
@ -7558,9 +7575,9 @@ of a configuration class:
@@ -7558,9 +7575,9 @@ of a configuration class:
If a `@Configuration` class is marked with `@Profile`, all of the `@Bean` methods and
`@Import` annotations associated with that class will be bypassed unless one or more of
the specified profiles are active. If a `@Component` or `@Configuration` class is marked
with `@Profile({"p1", "p2"})`, that class will not be registered/processed unless
profiles 'p1' and/or 'p2' have been activated. If a given profile is prefixed with the
NOT operator (`!`), the annotated element will be registered if the profile is **not**
active. For example, given `@Profile({"p1", "!p2"})`, registration will occur if profile
'p1' is active or if profile 'p2' is not active.
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:
@@ -7598,7 +7623,7 @@ configuration above can be rewritten in two XML files as follows:
@ -7637,7 +7662,7 @@ It is also possible to avoid that split and nest `<beans/>` elements within the
@@ -7637,7 +7662,7 @@ It is also possible to avoid that split and nest `<beans/>` elements within the
@ -7671,7 +7696,7 @@ it programmatically against the `Environment` API which is available via an
@@ -7671,7 +7696,7 @@ it programmatically against the `Environment` API which is available via an
[subs="verbatim,quotes"]
----
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();