Annotation Interface Bean
Used on methods and fields of BeanStore-managed objects to denote methods and fields that override and
customize beans used by the framework.
Example
The name()/value() attributes are used to differentiate between named beans.
Example
The methodScope() attribute is used to define beans in the scope of specific @RestOp-annotated methods.
Example
This annotation can also be used to inject arbitrary beans into the bean store which allows them to be
passed as resolved parameters on @RestOp-annotated methods.
Example
This annotation can also be used on uninitialized fields. When fields are uninitialized, they will be set during initialization based on beans found in the bean store.
Example
Notes:
- Methods and fields can be static or non-static.
- Any injectable beans (including spring beans) can be passed as arguments into methods.
- Bean names are required when multiple beans of the same type exist in the bean store.
@Beanmethods and fields may have any visibility (public, protected, package-private or private). Non-public members are made accessible before use, so the containing package must be opened to moduleorg.apache.juneau.commons when running on the module path.- By default, the injected bean scope is class-level (applies to the entire class). The
methodScope()annotation can be used to apply to method-level only (when applicable).
Bean names:
Each @Bean method registers its own result. Two methods returning the same type are distinguished by name,
and both beans are kept. Factory methods on
Example:
Precedence (since 10.0.0):
@Bean acts as a programmable default, analogous to Spring's
- Overriding-parent bean store — Spring beans (in
juneau-rest-server-springboot deployments, viaSpringBeanStore ), or any bean reachable through the configured overriding-parent bean-store chain. @Beanmethod/field on the resource class — registered as a regular bean-store entry, beating the framework default.- Memoizer-backed framework default — built into the context as a default supplier.
In other words: a Spring @Bean RichLogger
method on the same servlet, which in turn wins over the framework's built-in class-name-based default logger.
Non-Spring deployments have an empty overriding-parent layer, so the chain naturally collapses to
@Bean > default.
Prior to 10.0 the order was @Bean > Spring > default. See the 10.0.0 release notes for migration
guidance if you need to keep that legacy behavior (typically by removing the Spring @Bean method's type with a Spring-native override such as
See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionString[]Optional description for the exposed API.String[]The short names of the methods that this annotation applies to.The bean name to use to distinguish beans of the same type for different purposes.intOptional ordering value used when exporting ordered bean collections.Same asname().
-
Element Details
-
description
Optional description for the exposed API.- Returns:
- The annotation value.
- Since:
- 9.2.0
- Default:
- {}
-
methodScope
The short names of the methods that this annotation applies to.Can use
"*" to apply to all methods.Ignored for class-level scope.
- Returns:
- The short names of the methods that this annotation applies to, or empty if class-scope.
- Default:
- {}
-
name
The bean name to use to distinguish beans of the same type for different purposes.For example, there are two
HeaderListbeans:"defaultRequestHeaders" and"defaultResponseHeaders" . This annotation would be used to differentiate between them.- Returns:
- The bean name to use to distinguish beans of the same type for different purposes, or blank if bean type is unique.
- Default:
- ""
-
value
Same asname().- Returns:
- The bean name to use to distinguish beans of the same type for different purposes, or blank if bean type is unique.
- Default:
- ""
-
priority
int priorityOptional ordering value used when exporting ordered bean collections.Lower values are higher precedence. If both
Orderand this value are present on the same contribution source,Orderwins.- Returns:
- The priority value.
- Default:
- 1073741823
-