Close

Elements of @Bean Annotation

[Last Updated: Feb 13, 2026]

Definition of Bean

Version: 7.0.4
 package org.springframework.context.annotation;
 @Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 public @interface Bean {
     @AliasFor("name")
     String[] value() default {}; 1
     @AliasFor("value")
     String[] name() default {}; 2
     boolean autowireCandidate() default true; 3
     boolean defaultCandidate() default true; 4
     Bootstrap bootstrap() default Bootstrap.DEFAULT; 5
     String initMethod() default ""; 6
     String destroyMethod() default AbstractBeanDefinition.INFER_METHOD; 7
     enum Bootstrap { 8
         DEFAULT, 9
         BACKGROUND 10
     }
 }
1Alias for #name. (Since 4.3.3)
2The name of this bean, or if several names, a primary bean name plus aliases.
3Is this bean a candidate for getting autowired into some other bean at all? (Since 5.1)
4Is this bean a candidate for getting autowired into some other bean based on the plain type, without any further indications such as a qualifier match? (Since 6.2)
5The bootstrap mode for this bean: default is the main pre-instantiation thread for non-lazy singleton beans and the caller thread for prototype beans. (Since 6.2)
6The optional name of a method to call on the bean instance during initialization.
7The optional name of a method to call on the bean instance upon closing the application context, for example a close() method on a JDBC DataSource implementation, or a Hibernate SessionFactory object.
8Local enumeration for the bootstrap mode. (Since 6.2)
9Constant to indicate the main pre-instantiation thread for non-lazy singleton beans and the caller thread for prototype beans.
10Allow for instantiating a bean on a background thread.

The @Bean annotation is used on Java based configuration method. Following quick examples show basic usages:

  1. name :

    The optional bean name.

    @Configuration
    public class AppConfig {
    
        @Bean(name = "myBean")
        public MyBean createBean() {
           ......
        }
    }
            

    By default Spring container associate the bean with method name .

    A bean can be associated with multiple names, the extra ones are considered aliases.

    Specifying a unique meaningful name to a bean is necessary if the configuration provides more than one implementations for a bean. In that case, we have to use @Qualifier at the injection point which has an option to specify the bean name. An example here.

  2. initMethod/destroyMethod :

    Optional initialization and destruction callback method names.

    @Configuration
    public class AppConfig {
    
        @Bean(initMethod = "init", destroyMethod = "destroy")
        public MyBean createBean() {
         ......
        }
    }
    

    The destroy method will only be called for singletons but not for other scoped as Spring does not manage complete life cycle of the other beans.

    In case of singleton this method is called upon closing the application context.

    Since Spring also supports Java SE Common Annotations (JSR-250), we can annotate bean's methods with @PostConstruct and @PreDestroy instead of these elements. Please see an example here.

    There's one more annotation from JSR-250 that Spring supports, @Resource. Please see an example here.

See Also

Join