Skip to content

Commit 1d060ec

Browse files
committed
Document common use cases for @order vs @priority vs @dependsOn
Issue: SPR-16213 (cherry picked from commit 84699c8)
1 parent 122a3fe commit 1d060ec

File tree

4 files changed

+98
-42
lines changed
  • spring-beans/src/main/java/org/springframework/beans/factory/annotation
  • spring-context/src/main/java/org/springframework/context/annotation
  • spring-core/src/main/java/org/springframework/core/annotation
  • src/asciidoc

4 files changed

+98
-42
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/Autowired.java

+22-20
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,33 @@
2323
import java.lang.annotation.Target;
2424

2525
/**
26-
* Marks a constructor, field, setter method or config method as to be
27-
* autowired by Spring's dependency injection facilities.
26+
* Marks a constructor, field, setter method or config method as to be autowired
27+
* by Spring's dependency injection facilities.
2828
*
29-
* <p>Only one constructor (at max) of any given bean class may carry this
30-
* annotation, indicating the constructor to autowire when used as a Spring
31-
* bean. Such a constructor does not have to be public.
29+
* <p>Only one constructor (at max) of any given bean class may carry this annotation,
30+
* indicating the constructor to autowire when used as a Spring bean. Such a
31+
* constructor does not have to be public.
3232
*
33-
* <p>Fields are injected right after construction of a bean, before any
34-
* config methods are invoked. Such a config field does not have to be public.
33+
* <p>Fields are injected right after construction of a bean, before any config
34+
* methods are invoked. Such a config field does not have to be public.
3535
*
36-
* <p>Config methods may have an arbitrary name and any number of arguments;
37-
* each of those arguments will be autowired with a matching bean in the
38-
* Spring container. Bean property setter methods are effectively just
39-
* a special case of such a general config method. Such config methods
40-
* do not have to be public.
36+
* <p>Config methods may have an arbitrary name and any number of arguments; each of
37+
* those arguments will be autowired with a matching bean in the Spring container.
38+
* Bean property setter methods are effectively just a special case of such a general
39+
* config method. Such config methods do not have to be public.
4140
*
42-
* <p>In the case of multiple argument methods, the 'required' parameter is
43-
* applicable for all arguments.
41+
* <p>In the case of a multi-arg constructor or method, the 'required' parameter is
42+
* applicable to all arguments. Individual parameters may be declared as Java-8-style
43+
* {@link java.util.Optional}, overriding the base required semantics.
4444
*
45-
* <p>In case of a {@link java.util.Collection} or {@link java.util.Map}
46-
* dependency type, the container can autowire all beans matching the
47-
* declared value type. For such purposes, the map keys must be declared
48-
* as type String and will be resolved to the corresponding bean names.
49-
* Alternatively, a target bean may also be of type {@code Collection} or
50-
* {@code Map} itself, getting injected as such.
45+
* <p>In case of a {@link java.util.Collection} or {@link java.util.Map} dependency type,
46+
* the container autowires all beans matching the declared value type. For such purposes,
47+
* the map keys must be declared as type String which will be resolved to the corresponding
48+
* bean names. Such a container-provided collection will be ordered, taking into account
49+
* {@link org.springframework.core.Ordered}/{@link org.springframework.core.annotation.Order}
50+
* values of the target components, otherwise following their registration order in the
51+
* container. Alternatively, a single matching target bean may also be a generally typed
52+
* {@code Collection} or {@code Map} itself, getting injected as such.
5153
*
5254
* <p>Note that actual injection is performed through a
5355
* {@link org.springframework.beans.factory.config.BeanPostProcessor

spring-context/src/main/java/org/springframework/context/annotation/Bean.java

+34-6
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,50 @@
6060
* }
6161
* </pre>
6262
*
63-
* <h3>Scope, DependsOn, Primary, and Lazy</h3>
63+
* <h3>Profile, Scope, Lazy, DependsOn, Primary, Order</h3>
6464
*
65-
* <p>Note that the {@code @Bean} annotation does not provide attributes for scope,
66-
* depends-on, primary, or lazy. Rather, it should be used in conjunction with
67-
* {@link Scope @Scope}, {@link DependsOn @DependsOn}, {@link Primary @Primary},
68-
* and {@link Lazy @Lazy} annotations to achieve those semantics. For example:
65+
* <p>Note that the {@code @Bean} annotation does not provide attributes for profile,
66+
* scope, lazy, depends-on or primary. Rather, it should be used in conjunction with
67+
* {@link Scope @Scope}, {@link Lazy @Lazy}, {@link DependsOn @DependsOn} and
68+
* {@link Primary @Primary} annotations to declare those semantics. For example:
6969
*
7070
* <pre class="code">
7171
* &#064;Bean
72+
* &#064;Profile("production")
7273
* &#064;Scope("prototype")
7374
* public MyBean myBean() {
7475
* // instantiate and configure MyBean obj
7576
* return obj;
7677
* }
7778
* </pre>
7879
*
80+
* The semantics of the above-mentioned annotations match their use at the component
81+
* class level: {@code Profile} allows for selective inclusion of certain beans.
82+
* {@code @Scope} changes the bean's scope from singleton to the specified scope.
83+
* {@code @Lazy} only has an actual effect in case of the default singleton scope.
84+
* {@code @DependsOn} enforces the creation of specific other beans before this
85+
* bean will be created, in addition to any dependencies that the bean expressed
86+
* through direct references, which is typically helpful for singleton startup.
87+
* {@code @Primary} is a mechanism to resolve ambiguity at the injection point level
88+
* if a single target component needs to be injected but several beans match by type.
89+
*
90+
* <p>Additionally, {@code @Bean} methods may also declare qualifier annotations
91+
* and {@link org.springframework.core.annotation.Order @Order} values, to be
92+
* taken into account during injection point resolution just like corresponding
93+
* annotations on the corresponding component classes but potentially being very
94+
* individual per bean definition (in case of multiple definitions with the same
95+
* bean class). Qualifiers narrow the set of candidates after the initial type match;
96+
* order values determine the order of resolved elements in case of collection
97+
* injection points (with several target beans matching by type and qualifier).
98+
*
99+
* <p><b>NOTE:</b> {@code @Order} values may influence priorities at injection points
100+
* but please be aware that they do not influence singleton startup order which is an
101+
* orthogonal concern determined by dependency relationships and {@code @DependsOn}
102+
* declarations as mentioned above. Also, {@link javax.annotation.Priority} is not
103+
* available at this level since it cannot be declared on methods; its semantics can
104+
* be modelled through {@code @Order} values in combination with {@code @Primary} on
105+
* a single bean per type.
106+
*
79107
* <h3>{@code @Bean} Methods in {@code @Configuration} Classes</h3>
80108
*
81109
* <p>Typically, {@code @Bean} methods are declared within {@code @Configuration}
@@ -143,7 +171,7 @@
143171
*
144172
* <h3>Bootstrapping</h3>
145173
*
146-
* <p>See @{@link Configuration} Javadoc for further details including how to bootstrap
174+
* <p>See the @{@link Configuration} javadoc for further details including how to bootstrap
147175
* the container using {@link AnnotationConfigApplicationContext} and friends.
148176
*
149177
* <h3>{@code BeanFactoryPostProcessor}-returning {@code @Bean} methods</h3>

spring-core/src/main/java/org/springframework/core/annotation/Order.java

+21-13
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.
@@ -27,21 +27,29 @@
2727
/**
2828
* {@code @Order} defines the sort order for an annotated component.
2929
*
30-
* <p>The {@link #value} is optional and represents an order value as defined
31-
* in the {@link Ordered} interface. Lower values have higher priority. The
32-
* default value is {@code Ordered.LOWEST_PRECEDENCE}, indicating
33-
* lowest priority (losing to any other specified order value).
30+
* <p>The {@link #value} is optional and represents an order value as defined in the
31+
* {@link Ordered} interface. Lower values have higher priority. The default value is
32+
* {@code Ordered.LOWEST_PRECEDENCE}, indicating lowest priority (losing to any other
33+
* specified order value).
3434
*
35-
* <p>Since Spring 4.1, the standard {@link javax.annotation.Priority}
36-
* annotation can be used as a drop-in replacement for this annotation.
35+
* <p><b>NOTE:</b> Since Spring 4.0, annotation-based ordering is supported for many
36+
* kinds of components in Spring, even for collection injection where the order values
37+
* of the target components are taken into account (either from their target class or
38+
* from their {@code @Bean} method). While such order values may influence priorities
39+
* at injection points, please be aware that they do not influence singleton startup
40+
* order which is an orthogonal concern determined by dependency relationships and
41+
* {@code @DependsOn} declarations (influencing a runtime-determined dependency graph).
3742
*
38-
* <p><b>NOTE</b>: Annotation-based ordering is supported for specific kinds
39-
* of components only &mdash; for example, for annotation-based AspectJ
40-
* aspects. Ordering strategies within the Spring container, on the other
41-
* hand, are typically based on the {@link Ordered} interface in order to
42-
* allow for programmatically configurable ordering of each <i>instance</i>.
43+
* <p>Since Spring 4.1, the standard {@link javax.annotation.Priority} annotation
44+
* can be used as a drop-in replacement for this annotation in ordering scenarios.
45+
* Note that {@code Priority} may have additional semantics when a single element
46+
* has to be picked (see {@link AnnotationAwareOrderComparator#getPriority}).
4347
*
44-
* <p>Consult the Javadoc for {@link org.springframework.core.OrderComparator
48+
* <p>Alternatively, order values may also be determined on a per-instance basis
49+
* through the {@link Ordered} interface, allowing for configuration-determined
50+
* instance values instead of hard-coded values attached to a particular class.
51+
*
52+
* <p>Consult the javadoc for {@link org.springframework.core.OrderComparator
4553
* OrderComparator} for details on the sort semantics for non-ordered objects.
4654
*
4755
* @author Rod Johnson

src/asciidoc/core-beans.adoc

+21-3
Original file line numberDiff line numberDiff line change
@@ -4468,9 +4468,20 @@ The same applies for typed collections:
44684468

44694469
[TIP]
44704470
====
4471-
Your beans can implement the `org.springframework.core.Ordered` interface or either use
4471+
Your target beans can implement the `org.springframework.core.Ordered` interface or use
44724472
the `@Order` or standard `@Priority` annotation if you want items in the array or list
4473-
to be sorted into a specific order.
4473+
to be sorted into a specific order. Otherwise their order will follow the registration
4474+
order of the corresponding target bean definitions in the container.
4475+
4476+
The `@Order` annotation may be declared at target class level but also on `@Bean` methods,
4477+
potentially being very individual per bean definition (in case of multiple definitions
4478+
with the same bean class). `@Order` values may influence priorities at injection points
4479+
but please be aware that they do not influence singleton startup order which is an
4480+
orthogonal concern determined by dependency relationships and `@DependsOn` declarations.
4481+
4482+
Note that the standard `javax.annotation.Priority` annotation is not available at the
4483+
`@Bean` level since it cannot be declared on methods. Its semantics can be modelled
4484+
through `@Order` values in combination with `@Primary` on a single bean per type.
44744485
====
44754486

44764487
Even typed Maps can be autowired as long as the expected key type is `String`. The Map
@@ -6968,7 +6979,6 @@ another configuration class:
69686979
public A a() {
69696980
return new A();
69706981
}
6971-
69726982
}
69736983
69746984
@Configuration
@@ -7231,6 +7241,14 @@ way, navigating `@Configuration` classes and their dependencies becomes no diffe
72317241
than the usual process of navigating interface-based code.
72327242
--
72337243

7244+
[TIP]
7245+
====
7246+
If you would like to influence the startup creation order of certain beans, consider
7247+
declaring some of them as `@Lazy` (for creation on first access instead of on startup)
7248+
or as `@DependsOn` on certain other beans (making sure that specific other beans will
7249+
be created before the current bean, beyond what the latter's direct dependencies imply).
7250+
====
7251+
72347252

72357253
[[beans-java-conditional]]
72367254
==== Conditionally include @Configuration classes or @Bean methods

0 commit comments

Comments
 (0)