org.springframework.config.java.annotation
Annotation Type Bean


@Target(value=METHOD)
@Retention(value=RUNTIME)
@Inherited
@Documented
public @interface Bean

Annotation to be applied to methods that create beans in a Spring context. The name of the bean is the method name. (It is also possible to specify aliases using the aliases array on this annotation.)

Contains similar information to that held in Spring's internal BeanDefinition metadata.

Bean creation methods must be non-private (default, public or protected). Bean creation methods may throw any exception, which will be caught and handled by the Spring container on processing of the configuration class.
Bean creation methods must return an object type. The decision to return a class or an interface will be significant in the event of proxying. Bean methods that return interfaces will be proxied using dynamic proxies; those that return a class will require CGLIB or other subclass-based proxying. It is recommended to return an interface where possible, as this is also consistent with best practice around loose coupling.

Bean creation methods may reference other bean creation methods by calling them directly, as follows. This ensures that references between beans are strongly typed:

 @Bean
 public Company springSource() {
  Company company = new DefaultCompany("SpringSource");
  company.setChiefTechnologyOfficer(adrian());
  return company;
 }

 @Bean
 public Person adrian() {
  return new Person("Adrian Colyer");
 }
 

If a bean creation method is protected, rather than public, the the bean will be hidden. This means that the bean will be added to a child factory used internally by the ConfigurationProcessor, rather than the main factory, meaning it won't be visible to other definitions. This is particularly useful for Spring AOP Advisors or AspectJ aspects, which might otherwise alter behaviour of the owning factory as a whole.

Author:
Rod Johnson, Costin Leau, Chris Beams
See Also:
Configuration

Optional Element Summary
 java.lang.String[] aliases
          Bean aliases.
 boolean allowOverriding
          Allow the bean to be overridden in another JavaConfig, XML or other non-Java configuration.
 org.springframework.beans.factory.annotation.Autowire autowire
          Bean autowire strategy.
 DependencyCheck dependencyCheck
          Bean dependency check strategy.
 java.lang.String[] dependsOn
          Beans on which the current bean depends on.
 java.lang.String destroyMethodName
          Bean destroy method name.
 java.lang.String initMethodName
          Bean init method name.
 Lazy lazy
          Bean lazy strategy.
 Meta[] meta
          Metadata for the current bean.
 Primary primary
          A bean may be marked as primary, useful for disambiguation when looking up beans by type.
 int role
          Role this bean plays in the overall application configuration.
 java.lang.String scope
          Scope: whether the bean is a singleton, prototype or custom scope.
 

role

public abstract int role
Role this bean plays in the overall application configuration.

See Also:
BeanDefinition.ROLE_APPLICATION, BeanDefinition.ROLE_INFRASTRUCTURE, BeanDefinition.ROLE_SUPPORT, the 'role' field is assigned by default to ROLE_APPLICATION
Default:
0

aliases

public abstract java.lang.String[] aliases
Bean aliases.

Default:
{}

scope

public abstract java.lang.String scope
Scope: whether the bean is a singleton, prototype or custom scope. Default is singleton.

Default:
"singleton"

autowire

public abstract org.springframework.beans.factory.annotation.Autowire autowire
Bean autowire strategy.

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

lazy

public abstract Lazy lazy
Bean lazy strategy.

Default:
org.springframework.config.java.annotation.Lazy.UNSPECIFIED

primary

public abstract Primary primary
A bean may be marked as primary, useful for disambiguation when looking up beans by type.

See Also:
JavaConfigApplicationContext.getBean(Class);
Default:
org.springframework.config.java.annotation.Primary.UNSPECIFIED

initMethodName

public abstract java.lang.String initMethodName
Bean init method name. Normally this is not needed, as the initialization (with parameterization) can be done directly through java code.

Default:
""

destroyMethodName

public abstract java.lang.String destroyMethodName
Bean destroy method name.

Default:
""

dependencyCheck

public abstract DependencyCheck dependencyCheck
Bean dependency check strategy.

Default:
org.springframework.config.java.annotation.DependencyCheck.UNSPECIFIED

dependsOn

public abstract java.lang.String[] dependsOn
Beans on which the current bean depends on.

Default:
{}

meta

public abstract Meta[] meta
Metadata for the current bean.

Default:
{}

allowOverriding

public abstract boolean allowOverriding
Allow the bean to be overridden in another JavaConfig, XML or other non-Java configuration. This is consistent with DefaultListableBeanFactory's allowBeanDefinitionOverriding property, which defaults to true.

Returns:
whether overriding of this bean is allowed
Default:
true