org.springframework.context.annotation
Annotation Type Bean


@Target(value={METHOD,ANNOTATION_TYPE})
@Retention(value=RUNTIME)
@Documented
public @interface Bean

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 <bean/> element in the Spring XML schema. Deviations are as follows:

The Bean annotation does not provide attributes for scope, primary or lazy. Rather, it should be used in conjunction with Scope, Primary and Lazy annotations to acheive the same semantics.

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 may be used. Also note that name() accepts an array of strings. This is in order to allow for specifying multiple names (aka aliases) for a single bean.

Constraints

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.

Since:
3.0
Author:
Rod Johnson, Costin Leau, Chris Beams
See Also:
Configuration, Lazy, Primary, Scope

Optional Element Summary
 Autowire autowire
          Are dependencies to be injected via autowiring?
 String destroyMethod
          The optional name of a method to call on the bean instance during upon closing the application context, for example a close() method on a DataSource.
 String initMethod
          The optional name of a method to call on the bean instance during initialization.
 String[] name
          The name of this bean, or if plural, aliases for this bean.
 

name

public abstract String[] name
The name of this bean, or if plural, aliases for this bean. If left unspecified the name of the bean is the name of the annotated method. If specified, the method name is ignored.

Default:
{}

autowire

public abstract Autowire autowire
Are dependencies to be injected via autowiring?

Default:
org.springframework.beans.factory.annotation.Autowire.NO

initMethod

public abstract String initMethod
The optional name of a method to call on the bean instance during initialization. Not commonly used, given that the method may be called programmatically directly within the body of a Bean-annotated method.

Default:
""

destroyMethod

public abstract String destroyMethod
The optional name of a method to call on the bean instance during upon closing the application context, for example a close() method on a DataSource. The method must have no arguments, but may throw any exception.

Note: Only invoked on beans whose lifecycle is under the full control of the factory which is always the case for singletons, but not guaranteed for any other scope. see ConfigurableApplicationContext.close()

Default:
""