|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT | ||||||||
@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.
Note that 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 achieve
those semantics. The same annotations can also be used at the type level, e.g. for
component scanning.
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 (i.e., aliases) for a single bean.
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.
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.
BeanFactoryPostProcessor-returning @Bean methodsSpecial 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:
@Bean
public static PropertyPlaceholderConfigurer ppc() {
// instantiate, configure and return ppc ...
}
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.
Component,
Configuration,
Scope,
DependsOn,
Lazy,
Primary,
Autowired,
Value| 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 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. |
public abstract String[] name
public abstract Autowire autowire
public abstract String initMethod
public abstract String destroyMethod
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.
ConfigurableApplicationContext.close()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT | ||||||||