Skip to content

Commit 022aefd

Browse files
committed
Explicit note about @Profile declarations on overloaded @bean methods
Also marks @conditional as @documented, aligned with other annotations. Issue: SPR-15266
1 parent 9abf249 commit 022aefd

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

Diff for: spring-context/src/main/java/org/springframework/context/annotation/Conditional.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.context.annotation;
1818

19+
import java.lang.annotation.Documented;
1920
import java.lang.annotation.ElementType;
2021
import java.lang.annotation.Retention;
2122
import java.lang.annotation.RetentionPolicy;
@@ -55,8 +56,9 @@
5556
* @since 4.0
5657
* @see Condition
5758
*/
58-
@Retention(RetentionPolicy.RUNTIME)
5959
@Target({ElementType.TYPE, ElementType.METHOD})
60+
@Retention(RetentionPolicy.RUNTIME)
61+
@Documented
6062
public @interface Conditional {
6163

6264
/**

Diff for: spring-context/src/main/java/org/springframework/context/annotation/Profile.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,12 +57,19 @@
5757
*
5858
* <p>If a given profile is prefixed with the NOT operator ({@code !}), the annotated
5959
* component will be registered if the profile is <em>not</em> active &mdash; for example,
60-
* given {@code @Profile({"p1", "!p2"})}, registration will occur if profile 'p1' is active or
61-
* if profile 'p2' is <em>not</em> active.
60+
* given {@code @Profile({"p1", "!p2"})}, registration will occur if profile 'p1' is active
61+
* or if profile 'p2' is <em>not</em> active.
6262
*
6363
* <p>If the {@code @Profile} annotation is omitted, registration will occur regardless
6464
* of which (if any) profiles are active.
6565
*
66+
* <p><b>NOTE:</b> With {@code @Profile} on {@code @Bean} methods, a special scenario may
67+
* apply: In the case of overloaded {@code @Bean} methods, all {@code @Profile} declarations
68+
* from all applicable factory methods for the same bean will be merged; as a consequence,
69+
* they all need to match for the bean to become registered. {@code @Profile} can therefore
70+
* not be used to select a particular overloaded method over another; resolution between
71+
* overloaded factory methods only follows Spring's constructor resolution algorithm.
72+
*
6673
* <p>When defining Spring beans via XML, the {@code "profile"} attribute of the
6774
* {@code <beans>} element may be used. See the documentation in the
6875
* {@code spring-beans} XSD (version 3.1 or greater) for details.
@@ -78,8 +85,8 @@
7885
* @see Conditional
7986
* @see org.springframework.test.context.ActiveProfiles
8087
*/
81-
@Retention(RetentionPolicy.RUNTIME)
8288
@Target({ElementType.TYPE, ElementType.METHOD})
89+
@Retention(RetentionPolicy.RUNTIME)
8390
@Documented
8491
@Conditional(ProfileCondition.class)
8592
public @interface Profile {

Diff for: src/docs/asciidoc/core/core-beans.adoc

+26-10
Original file line numberDiff line numberDiff line change
@@ -7416,6 +7416,9 @@ jdbc.password=
74167416
}
74177417
----
74187418

7419+
7420+
7421+
74197422
[[beans-environment]]
74207423
== Environment abstraction
74217424

@@ -7437,6 +7440,8 @@ on. The role of the `Environment` object with relation to properties is to provi
74377440
user with a convenient service interface for configuring property sources and resolving
74387441
properties from them.
74397442

7443+
7444+
74407445
[[beans-definition-profiles]]
74417446
=== Bean definition profiles
74427447

@@ -7563,6 +7568,18 @@ of creating a custom _composed annotation_. The following example defines a cust
75637568
}
75647569
----
75657570

7571+
[TIP]
7572+
====
7573+
If a `@Configuration` class is marked with `@Profile`, all of the `@Bean` methods and
7574+
`@Import` annotations associated with that class will be bypassed unless one or more of
7575+
the specified profiles are active. If a `@Component` or `@Configuration` class is marked
7576+
with `@Profile({"p1", "p2"})`, that class will not be registered/processed unless
7577+
profiles 'p1' and/or 'p2' have been activated. If a given profile is prefixed with the
7578+
NOT operator (`!`), the annotated element will be registered if the profile is **not**
7579+
active. For example, given `@Profile({"p1", "!p2"})`, registration will occur if profile
7580+
'p1' is active or if profile 'p2' is not active.
7581+
====
7582+
75667583
`@Profile` can also be declared at the method level to include only one particular bean
75677584
of a configuration class:
75687585

@@ -7591,20 +7608,19 @@ of a configuration class:
75917608
}
75927609
----
75937610

7594-
[TIP]
7611+
[NOTE]
75957612
====
7596-
If a `@Configuration` class is marked with `@Profile`, all of the `@Bean` methods and
7597-
`@Import` annotations associated with that class will be bypassed unless one or more of
7598-
the specified profiles are active. If a `@Component` or `@Configuration` class is marked
7599-
with `@Profile({"p1", "p2"})`, that class will not be registered/processed unless
7600-
profiles 'p1' and/or 'p2' have been activated. If a given profile is prefixed with the
7601-
NOT operator (`!`), the annotated element will be registered if the profile is **not**
7602-
active. For example, given `@Profile({"p1", "!p2"})`, registration will occur if profile
7603-
'p1' is active or if profile 'p2' is not active.
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.
76047619
====
76057620

7621+
76067622
[[beans-definition-profiles-xml]]
7607-
=== XML bean definition profiles
7623+
==== XML bean definition profiles
76087624

76097625
The XML counterpart is the `profile` attribute of the `<beans>` element. Our sample
76107626
configuration above can be rewritten in two XML files as follows:

0 commit comments

Comments
 (0)