|
|
|
@ -60,15 +60,16 @@ import org.springframework.core.annotation.AliasFor; |
|
|
|
* } |
|
|
|
* } |
|
|
|
* </pre> |
|
|
|
* </pre> |
|
|
|
* |
|
|
|
* |
|
|
|
* <h3>Scope, DependsOn, Primary, and Lazy</h3> |
|
|
|
* <h3>Profile, Scope, Lazy, DependsOn, Primary, Order</h3> |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>Note that the {@code @Bean} annotation does not provide attributes for scope, |
|
|
|
* <p>Note that the {@code @Bean} annotation does not provide attributes for profile, |
|
|
|
* depends-on, primary, or lazy. Rather, it should be used in conjunction with |
|
|
|
* scope, lazy, depends-on or primary. Rather, it should be used in conjunction with |
|
|
|
* {@link Scope @Scope}, {@link DependsOn @DependsOn}, {@link Primary @Primary}, |
|
|
|
* {@link Scope @Scope}, {@link Lazy @Lazy}, {@link DependsOn @DependsOn} and |
|
|
|
* and {@link Lazy @Lazy} annotations to achieve those semantics. For example: |
|
|
|
* {@link Primary @Primary} annotations to declare those semantics. For example: |
|
|
|
* |
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* @Bean |
|
|
|
* @Bean |
|
|
|
|
|
|
|
* @Profile("production") |
|
|
|
* @Scope("prototype") |
|
|
|
* @Scope("prototype") |
|
|
|
* public MyBean myBean() { |
|
|
|
* public MyBean myBean() { |
|
|
|
* // instantiate and configure MyBean obj
|
|
|
|
* // instantiate and configure MyBean obj
|
|
|
|
@ -76,6 +77,33 @@ import org.springframework.core.annotation.AliasFor; |
|
|
|
* } |
|
|
|
* } |
|
|
|
* </pre> |
|
|
|
* </pre> |
|
|
|
* |
|
|
|
* |
|
|
|
|
|
|
|
* The semantics of the above-mentioned annotations match their use at the component |
|
|
|
|
|
|
|
* class level: {@code Profile} allows for selective inclusion of certain beans. |
|
|
|
|
|
|
|
* {@code @Scope} changes the bean's scope from singleton to the specified scope. |
|
|
|
|
|
|
|
* {@code @Lazy} only has an actual effect in case of the default singleton scope. |
|
|
|
|
|
|
|
* {@code @DependsOn} enforces the creation of specific other beans before this |
|
|
|
|
|
|
|
* bean will be created, in addition to any dependencies that the bean expressed |
|
|
|
|
|
|
|
* through direct references, which is typically helpful for singleton startup. |
|
|
|
|
|
|
|
* {@code @Primary} is a mechanism to resolve ambiguity at the injection point level |
|
|
|
|
|
|
|
* if a single target component needs to be injected but several beans match by type. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* <p>Additionally, {@code @Bean} methods may also declare qualifier annotations |
|
|
|
|
|
|
|
* and {@link org.springframework.core.annotation.Order @Order} values, to be |
|
|
|
|
|
|
|
* taken into account during injection point resolution just like corresponding |
|
|
|
|
|
|
|
* annotations on the corresponding component classes but potentially being very |
|
|
|
|
|
|
|
* individual per bean definition (in case of multiple definitions with the same |
|
|
|
|
|
|
|
* bean class). Qualifiers narrow the set of candidates after the initial type match; |
|
|
|
|
|
|
|
* order values determine the order of resolved elements in case of collection |
|
|
|
|
|
|
|
* injection points (with several target beans matching by type and qualifier). |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* <p><b>NOTE:</b> {@code @Order} values may influence priorities at injection points |
|
|
|
|
|
|
|
* but please be aware that they do not influence singleton startup order which is an |
|
|
|
|
|
|
|
* orthogonal concern determined by dependency relationships and {@code @DependsOn} |
|
|
|
|
|
|
|
* declarations as mentioned above. Also, {@link javax.annotation.Priority} is not |
|
|
|
|
|
|
|
* available at this level since it cannot be declared on methods; its semantics can |
|
|
|
|
|
|
|
* be modelled through {@code @Order} values in combination with {@code @Primary} on |
|
|
|
|
|
|
|
* a single bean per type. |
|
|
|
|
|
|
|
* |
|
|
|
* <h3>{@code @Bean} Methods in {@code @Configuration} Classes</h3> |
|
|
|
* <h3>{@code @Bean} Methods in {@code @Configuration} Classes</h3> |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>Typically, {@code @Bean} methods are declared within {@code @Configuration} |
|
|
|
* <p>Typically, {@code @Bean} methods are declared within {@code @Configuration} |
|
|
|
@ -143,7 +171,7 @@ import org.springframework.core.annotation.AliasFor; |
|
|
|
* |
|
|
|
* |
|
|
|
* <h3>Bootstrapping</h3> |
|
|
|
* <h3>Bootstrapping</h3> |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>See @{@link Configuration} Javadoc for further details including how to bootstrap |
|
|
|
* <p>See the @{@link Configuration} javadoc for further details including how to bootstrap |
|
|
|
* the container using {@link AnnotationConfigApplicationContext} and friends. |
|
|
|
* the container using {@link AnnotationConfigApplicationContext} and friends. |
|
|
|
* |
|
|
|
* |
|
|
|
* <h3>{@code BeanFactoryPostProcessor}-returning {@code @Bean} methods</h3> |
|
|
|
* <h3>{@code BeanFactoryPostProcessor}-returning {@code @Bean} methods</h3> |
|
|
|
|