Learnitweb

Spring @Bean annotation

1. Introduction

In this tutorial, we’ll discuss @Bean annotation which is a very important annotation to know.

A Java object instance managed by Spring container is called bean. This annotation is typically used to define methods to create beans.

Following is the declaration of @Bean annotation:

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

This annotation is used on a method and indicates that the method produces a bean to be managed by the Spring container. Following is a typical example of using @Bean annotation:

@Bean
public MyBean myBean() {
    // instantiate and configure MyBean obj
    return obj;
} 

@Bean annotation does not provide attributes for profile, scope, lazy, depends-on or primary. If you need these then @Scope, @Lazy, @DependsOn and @Primary annotations are available separately. For example:

@Bean
@Profile("production")
@Scope("prototype")
public MyBean myBean() {
	// instantiate and configure MyBean obj
    return obj;
}

2. Bean names

By default the name of the bean is the name of the method. However, the @Bean annotation provides a name() attribute. The name() attribute can be used to provide name and alias explicitly for the bean. The name() attribute accepts an array of Strings, i.e. a primary bean name plus one or more aliases for a single bean.

@Bean({"name1", "alias1"}) // Both 'name1' and 'alias1' are valid names for bean
public MyBean myBean() {
	// instantiate and configure MyBean obj
	return obj;
}

3. @Bean Methods in @Configuration Classes

Typically, @Bean methods are declared within @Configuration classes. One @Bean method can call another @Bean method. These method calls are called as ‘inter-bean references‘.

 @Configuration
 public class AppConfig {

     @Bean
     public MyService myService() {
         return new MyService(myRepository());
     }

     @Bean
     public MyRepository myRepository() {
         return new JdbcMyRepository(dataSource());
     }

     // ...
 }

4. @Bean Lite Mode

Typically, @Bean methods are declared in @Configuration classes. However, it is not mandatory. Bean methods may be declared even in a plain old class or a class not declared with @Configuration such as @Component. In such cases, a @Bean method will get processed in a so-called ‘lite‘ mode. 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 ‘inter-bean references’ are not supported in lite mode.

5. BeanFactoryPostProcessor-returning @Bean methods

@Bean method which return BeanFactoryPostProcessor types should be declared as static. This is because these methods should be instantiated very early in the container lifecycle else these can create lifecycle issues. By marking this method as static, such methods can be invoked without causing instantiation of its declaring @Configuration class, thus avoiding the above-mentioned lifecycle conflicts.