@Bean
is a method-level annotation and a direct analog of the XML <bean/>
element. The annotation supports most of the attributes offered by <bean/>
, such as: init-method
, destroy-method
, autowiring
, lazy-init
, dependency-check
, depends-on
and scope
.
To declare a bean, simply annotate a method with the
@Bean
annotation. When JavaConfig encounters such a method, it will execute that method and register the return value as a bean within a BeanFactory
. By default, the bean name will be the same as the method name (see bean naming for details on how to customize this behavior). The following is a simple example of a @Bean
method declaration:@Configuration public class AppConfig { @Bean public TransferService transferService() { return new TransferServiceImpl(); } }
For comparison sake, the configuration above is exactly equivalent to the following Spring XML:
<beans> <bean name="transferService" class="com.acme.TransferServiceImpl"/> </beans>
Both will result in a bean named
transferService
being available in the BeanFactory
/ ApplicationContext
, bound to an object instance of type TransferServiceImpl
:transferService -> com.acme.TransferServiceImpl
When
@Bean
s have dependencies on one another, expressing that dependency is as simple as having one bean method call another:@Configuration public class AppConfig { @Bean public Foo foo() { return new Foo(bar()); } @Bean public Bar bar() { return new Bar(); } }
In the example above, the
foo
bean recevies a reference to bar
via constructor injection.
JavaConfig, like the core Spring Framework, supports use of JSR-250 "Common Annotations". For example:
public class FooService { @PostConstruct public void init() { // custom initialization logic } } @Configuration @AnnotationDrivenConfig public class ApplicationConfig { @Bean public FooService fooService() { return new FooService(); } }
In the above example,
FooService
declares @PostConstruct
. By declaring JavaConfig's @AnnotationDrivenConfig
on The @Configuration
class, this annotation will be respected by the container and called immediately after construction. See The core framework documentation on support for JSR-250 annotations for further details.
Spring's lifecycle callbacks are fully supported. If a bean implements
InitializingBean
, DisposableBean
, or Lifecycle
, their respective methods will be called by the container in accordance with their Javadoc.
The
@Bean
annotation supports specifying arbitrary initialization and destruction callback methods, much like Spring XML's init-method
and destroy-method
attributes to the bean
element:public class Foo { public void init() { // initialization logic } } public class Bar { public void cleanup() { // destruction logic } } @Configuration public class AppConfig { @Bean(initMethodName="init") public Foo foo() { return new Foo(); } @Bean(destroyMethodName="cleanup") public Bar bar() { return new Bar(); } }
Of course, in the case of
Foo
above, it would be equally as valid to call the init()
method directly during construction:@Configuration public class AppConfig { @Bean public Foo foo() { Foo foo = new Foo(); foo.init(); return foo; } // ... }
Tip | |
---|---|
Remember that because you are working directly in Java, you can do anything you like with your objects, and do not always need to rely on the container! |
No comments:
Post a Comment