Introduce 'value' alias for @Bean's 'name' attribute

In order to simplify configuration for use cases involving @Bean where
only a bean name or aliases are supplied as an attribute, this commit
introduces a new 'value' attribute that is an @AliasFor 'name' in @Bean.

Issue: SPR-14728
This commit is contained in:
Sam Brannen
2016-09-17 16:01:08 +02:00
parent 778ef02680
commit 8f62b63663
4 changed files with 114 additions and 53 deletions

View File

@@ -24,6 +24,7 @@ import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.core.annotation.AliasFor;
/**
* Indicates that a method produces a bean to be managed by the Spring container.
@@ -44,15 +45,15 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition;
*
* <h3>Bean Names</h3>
*
* <p>While a {@link #name() name} attribute is available, the default strategy for
* determining the name of a bean is to use the name of the {@code @Bean} method.
* This is convenient and intuitive, but if explicit naming is desired, the
* {@code name} attribute may be used. Also note that {@code name} accepts an array
* of Strings. This is in order to allow for specifying multiple names (i.e., aliases)
* for a single bean.
* <p>While a {@link #name} attribute is available, the default strategy for
* determining the name of a bean is to use the name of the {@code @Bean} method. This
* is convenient and intuitive, but if explicit naming is desired, the {@code name}
* attribute (or its alias {@code value}) may be used. Also note that {@code name}
* accepts an array of Strings. This is in order to allow for specifying multiple names
* (i.e., aliases) for a single bean.
*
* <pre class="code">
* &#064;Bean(name={"b1","b2"}) // bean available as 'b1' and 'b2', but not 'myBean'
* &#064;Bean({"b1","b2"}) // bean available as 'b1' and 'b2', but not 'myBean'
* public MyBean myBean() {
* // instantiate and configure MyBean obj
* return obj;
@@ -190,10 +191,24 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition;
public @interface Bean {
/**
* The name of this bean, or if plural, aliases for this bean. If left unspecified
* the name of the bean is the name of the annotated method. If specified, the method
* name is ignored.
* Alias for {@link #name}.
* <p>Intended to be used when no other attributes are needed, for example:
* {@code @Bean("customBeanName")}.
* @since 5.0
* @see #name
*/
@AliasFor("name")
String[] value() default {};
/**
* The name of this bean, or if plural, aliases for this bean.
* <p>If left unspecified the name of the bean is the name of the annotated method.
* If specified, the method name is ignored.
* <p>The bean name and aliases may also be configured via the {@link #value}
* attribute if no other attributes are declared.
* @see #value
*/
@AliasFor("value")
String[] name() default {};
/**