diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/Bean.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/Bean.java index bd58f61592dc4c29d729da8a388526cc75559daa..e2728d569b5c7c274ab8b4b7bb8e5306be9d4053 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/Bean.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/Bean.java @@ -27,46 +27,50 @@ import org.springframework.beans.factory.annotation.Autowire; /** * Indicates that a method produces a bean to be managed by the Spring container. The * names and semantics of the attributes to this annotation are intentionally similar - * to those of the {@literal } element in the Spring XML schema. Deviations are - * as follows: + * to those of the {@literal } element in the Spring XML schema. * - *

The Bean annotation does not provide attributes for scope, primary or lazy. Rather, - * it should be used in conjunction with {@link Scope @Scope}, - * {@link Primary @Primary}, and {@link Lazy @Lazy} annotations to achieve the - * same semantics. + *

Note that the @Bean annotation does not provide attributes for scope, + * primary or lazy. Rather, it should be used in conjunction with {@link Scope @Scope}, + * {@link Primary @Primary}, and {@link Lazy @Lazy} annotations to achieve + * those semantics. The same annotations can also be used at the type level, e.g. for + * component scanning. * *

While a {@link #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 {@link #name()} attribute may be used. - * Also note that {@link #name()} accepts an array of strings. This is in order to allow + * Also note that {@link #name()} accepts an array of Strings. This is in order to allow * for specifying multiple names (i.e., aliases) for a single bean. * - *

Constraints

- * + *

The @Bean annotation may be used on any methods in an @Component + * class, in which case they will get processed in a configuration class 'lite' mode where + * they will simply be called as plain factory methods from the container (similar to + * factory-method declarations in XML). The containing component classes remain + * unmodified in this case, and there are no unusual constraints for factory methods. * - *

Usage

- *

Bean methods may reference other Bean methods by calling them directly. This ensures - * that references between beans are strongly typed and navigable. So called 'inter-bean - * references' are guaranteed to respect scoping and AOP semantics. + *

As an advanced mode, @Bean may also be used within @Configuration + * component classes. In this case, bean methods may reference other @Bean methods + * on 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. * * @author Rod Johnson * @author Costin Leau * @author Chris Beams + * @author Juergen Hoeller * @since 3.0 + * @see org.springframework.stereotype.Component * @see Configuration + * @see Scope * @see DependsOn * @see Lazy * @see Primary - * @see Scope - * @see Value + * @see org.springframework.beans.factory.annotation.Autowired + * @see org.springframework.beans.factory.annotation.Value */ -@Target( { ElementType.METHOD, ElementType.ANNOTATION_TYPE }) +@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Bean {