1. org.springframework.context.annotation @Target({ElementType.METHOD,ElementType.ANNOTATION_TYPE})
    2. @Retention(RetentionPolicy.RUNTIME)
    3. @Documented
    4. public interface Bean
    5. extends annotation.Annotation
    6. Indicates that a method produces a bean to be managed by the Spring container.
    7. Overview
    8. The names and semantics of the attributes to this annotation are intentionally similar to those of the <bean/> element in the Spring XML schema. For example:
    9. @Bean
    10. public MyBean myBean() {
    11. // instantiate and configure MyBean obj
    12. return obj;
    13. }
    14. Bean Names
    15. While a name attribute is available, the default strategy for determining the name of a bean is to use the name of the @Bean method. This is convenient and intuitive, but if explicit naming is desired, the name attribute (or its alias value) may be used. Also note that name accepts an array of Strings, allowing for multiple names (i.e. a primary bean name plus one or more aliases) for a single bean.
    16. @Bean({"b1", "b2"}) // bean available as 'b1' and 'b2', but not 'myBean'
    17. public MyBean myBean() {
    18. // instantiate and configure MyBean obj
    19. return obj;
    20. }
    21. Profile, Scope, Lazy, DependsOn, Primary, Order
    22. Note that the @Bean annotation does not provide attributes for profile, scope, lazy, depends-on or primary. Rather, it should be used in conjunction with @Scope, @Lazy, @DependsOn and @Primary annotations to declare those semantics. For example:
    23. @Bean
    24. @Profile("production")
    25. @Scope("prototype")
    26. public MyBean myBean() {
    27. // instantiate and configure MyBean obj
    28. return obj;
    29. }
    30. The semantics of the above-mentioned annotations match their use at the component class level: @Profile allows for selective inclusion of certain beans. @Scope changes the bean's scope from singleton to the specified scope. @Lazy only has an actual effect in case of the default singleton scope. @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. @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.
    31. Additionally, @Bean methods may also declare qualifier annotations and @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).
    32. NOTE: @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 @DependsOn declarations as mentioned above. Also, javax.annotation.Priority is not available at this level since it cannot be declared on methods; its semantics can be modeled through @Order values in combination with @Primary on a single bean per type.
    33. @Bean Methods in @Configuration Classes
    34. Typically, @Bean methods are declared within @Configuration classes. In this case, bean methods may reference other @Bean methods in the same class by calling them directly. This ensures that references between beans are strongly typed and navigable. Such so-called 'inter-bean references' are guaranteed to respect scoping and AOP semantics, just like getBean() lookups would. These are the semantics known from the original 'Spring JavaConfig' project which require CGLIB subclassing of each such configuration class at runtime. As a consequence, @Configuration classes and their factory methods must not be marked as final or private in this mode. For example:
    35. @Configuration
    36. public class AppConfig {
    37. @Bean
    38. public FooService fooService() {
    39. return new FooService(fooRepository());
    40. }
    41. @Bean
    42. public FooRepository fooRepository() {
    43. return new JdbcFooRepository(dataSource());
    44. }
    45. // ...
    46. }
    47. @Bean Lite Mode
    48. @Bean methods may also be declared within classes that are not annotated with @Configuration. For example, bean methods may be declared in a @Component class or even in a plain old class. In such cases, a @Bean method will get processed in a so-called 'lite' mode.
    49. Bean methods in lite mode will be treated as plain factory methods by the container (similar to factory-method declarations in XML), with scoping and lifecycle callbacks properly applied. The containing class remains unmodified in this case, and there are no unusual constraints for the containing class or the factory methods.
    50. In contrast to the semantics for bean methods in @Configuration classes, 'inter-bean references' are not supported in lite mode. Instead, when one @Bean-method invokes another @Bean-method in lite mode, the invocation is a standard Java method invocation; Spring does not intercept the invocation via a CGLIB proxy. This is analogous to inter-@Transactional method calls where in proxy mode, Spring does not intercept the invocation Spring does so only in AspectJ mode.
    51. For example:
    52. @Component
    53. public class Calculator {
    54. public int sum(int a, int b) {
    55. return a+b;
    56. }
    57. @Bean
    58. public MyBean myBean() {
    59. return new MyBean();
    60. }
    61. }
    62. Bootstrapping
    63. See the @Configuration javadoc for further details including how to bootstrap the container using AnnotationConfigApplicationContext and friends.
    64. BeanFactoryPostProcessor-returning @Bean methods
    65. Special consideration must be taken for @Bean methods that return Spring BeanFactoryPostProcessor (BFPP) types. Because BFPP objects must be instantiated very early in the container lifecycle, they can interfere with processing of annotations such as @Autowired, @Value, and @PostConstruct within @Configuration classes. To avoid these lifecycle issues, mark BFPP-returning @Bean methods as static. For example:
    66. @Bean
    67. public static PropertySourcesPlaceholderConfigurer pspc() {
    68. // instantiate, configure and return pspc ...
    69. }
    70. By marking this method as static, it can be invoked without causing instantiation of its declaring @Configuration class, thus avoiding the above-mentioned lifecycle conflicts. Note however that static @Bean methods will not be enhanced for scoping and AOP semantics as mentioned above. This works out in BFPP cases, as they are not typically referenced by other @Bean methods. As a reminder, a WARN-level log message will be issued for any non-static @Bean methods having a return type assignable to BeanFactoryPostProcessor.
    71. Since:
    72. 3.0