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

@@ -20,9 +20,13 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import javax.inject.Provider;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
@@ -65,6 +69,7 @@ import static org.junit.Assert.*;
*
* @author Chris Beams
* @author Juergen Hoeller
* @author Sam Brannen
*/
public class ConfigurationClassProcessingTests {
@@ -92,40 +97,57 @@ public class ConfigurationClassProcessingTests {
}
@Test
public void customBeanNameIsRespected() {
GenericApplicationContext ac = new GenericApplicationContext();
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
ac.registerBeanDefinition("config", new RootBeanDefinition(ConfigWithBeanWithCustomName.class));
ac.refresh();
assertSame(ac.getBean("customName"), ConfigWithBeanWithCustomName.testBean);
@Rule
public final ExpectedException exception = ExpectedException.none();
// method name should not be registered
try {
ac.getBean("methodName");
fail("bean should not have been registered with 'methodName'");
}
catch (NoSuchBeanDefinitionException ex) {
// expected
}
@Test
public void customBeanNameIsRespectedWhenConfiguredViaNameAttribute() {
customBeanNameIsRespected(ConfigWithBeanWithCustomName.class,
() -> ConfigWithBeanWithCustomName.testBean, "customName");
}
@Test
public void aliasesAreRespected() {
BeanFactory factory = initBeanFactory(ConfigWithBeanWithAliases.class);
assertSame(factory.getBean("name1"), ConfigWithBeanWithAliases.testBean);
String[] aliases = factory.getAliases("name1");
for (String alias : aliases)
assertSame(factory.getBean(alias), ConfigWithBeanWithAliases.testBean);
public void customBeanNameIsRespectedWhenConfiguredViaValueAttribute() {
customBeanNameIsRespected(ConfigWithBeanWithCustomNameConfiguredViaValueAttribute.class,
() -> ConfigWithBeanWithCustomNameConfiguredViaValueAttribute.testBean, "enigma");
}
private void customBeanNameIsRespected(Class<?> testClass, Supplier<TestBean> testBeanSupplier, String beanName) {
GenericApplicationContext ac = new GenericApplicationContext();
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
ac.registerBeanDefinition("config", new RootBeanDefinition(testClass));
ac.refresh();
assertSame(testBeanSupplier.get(), ac.getBean(beanName));
// method name should not be registered
try {
factory.getBean("methodName");
fail("bean should not have been registered with 'methodName'");
}
catch (NoSuchBeanDefinitionException ex) {
// expected
}
exception.expect(NoSuchBeanDefinitionException.class);
ac.getBean("methodName");
}
@Test
public void aliasesAreRespectedWhenConfiguredViaNameAttribute() {
aliasesAreRespected(ConfigWithBeanWithAliases.class,
() -> ConfigWithBeanWithAliases.testBean, "name1");
}
@Test
public void aliasesAreRespectedWhenConfiguredViaValueAttribute() {
aliasesAreRespected(ConfigWithBeanWithAliasesConfiguredViaValueAttribute.class,
() -> ConfigWithBeanWithAliasesConfiguredViaValueAttribute.testBean, "enigma");
}
private void aliasesAreRespected(Class<?> testClass, Supplier<TestBean> testBeanSupplier, String beanName) {
TestBean testBean = testBeanSupplier.get();
BeanFactory factory = initBeanFactory(testClass);
assertSame(testBean, factory.getBean(beanName));
Arrays.stream(factory.getAliases(beanName)).map(factory::getBean).forEach(alias -> assertSame(testBean, alias));
// method name should not be registered
exception.expect(NoSuchBeanDefinitionException.class);
factory.getBean("methodName");
}
@Test // SPR-11830
@@ -146,8 +168,9 @@ public class ConfigurationClassProcessingTests {
assertSame(ac.getBean("customName"), ConfigWithSetWithProviderImplementation.set);
}
@Test(expected = BeanDefinitionParsingException.class)
@Test
public void testFinalBeanMethod() {
exception.expect(BeanDefinitionParsingException.class);
initBeanFactory(ConfigWithFinalBean.class);
}
@@ -219,6 +242,7 @@ public class ConfigurationClassProcessingTests {
adaptive = factory.getBean(AdaptiveInjectionPoints.class);
assertEquals("adaptiveInjectionPoint1", adaptive.adaptiveInjectionPoint1.getName());
assertEquals("setAdaptiveInjectionPoint2", adaptive.adaptiveInjectionPoint2.getName());
factory.close();
}
@Test
@@ -240,27 +264,49 @@ public class ConfigurationClassProcessingTests {
SpousyTestBean listener = factory.getBean("listenerTestBean", SpousyTestBean.class);
assertTrue(listener.refreshed);
factory.close();
}
@Configuration
static class ConfigWithBeanWithCustomName {
static TestBean testBean = new TestBean();
static TestBean testBean = new TestBean(ConfigWithBeanWithCustomName.class.getSimpleName());
@Bean(name="customName")
@Bean(name = "customName")
public TestBean methodName() {
return testBean;
}
}
@Configuration
static class ConfigWithBeanWithCustomNameConfiguredViaValueAttribute {
static TestBean testBean = new TestBean(ConfigWithBeanWithCustomNameConfiguredViaValueAttribute.class.getSimpleName());
@Bean("enigma")
public TestBean methodName() {
return testBean;
}
}
@Configuration
static class ConfigWithBeanWithAliases {
static TestBean testBean = new TestBean();
static TestBean testBean = new TestBean(ConfigWithBeanWithAliases.class.getSimpleName());
@Bean(name={"name1", "alias1", "alias2", "alias3"})
@Bean(name = { "name1", "alias1", "alias2", "alias3" })
public TestBean methodName() {
return testBean;
}
}
@Configuration
static class ConfigWithBeanWithAliasesConfiguredViaValueAttribute {
static TestBean testBean = new TestBean(ConfigWithBeanWithAliasesConfiguredViaValueAttribute.class.getSimpleName());
@Bean({ "enigma", "alias1", "alias2", "alias3" })
public TestBean methodName() {
return testBean;
}
@@ -270,9 +316,9 @@ public class ConfigurationClassProcessingTests {
@Configuration
static class ConfigWithBeanWithProviderImplementation implements Provider<TestBean> {
static TestBean testBean = new TestBean();
static TestBean testBean = new TestBean(ConfigWithBeanWithProviderImplementation.class.getSimpleName());
@Bean(name="customName")
@Bean(name = "customName")
public TestBean get() {
return testBean;
}
@@ -284,7 +330,7 @@ public class ConfigurationClassProcessingTests {
static Set<String> set = Collections.singleton("value");
@Bean(name="customName")
@Bean(name = "customName")
public Set<String> get() {
return set;
}

View File

@@ -228,7 +228,7 @@ public class EnableMBeanExportConfigurationTests {
return new MBeanServerFactoryBean();
}
@Bean(name="bean:name=testBean4")
@Bean("bean:name=testBean4")
@Lazy
public AnnotationTestBean testBean4() {
AnnotationTestBean bean = new AnnotationTestBean();
@@ -237,7 +237,7 @@ public class EnableMBeanExportConfigurationTests {
return bean;
}
@Bean(name="bean:name=testBean5")
@Bean("bean:name=testBean5")
public AnnotationTestBeanFactory testBean5() throws Exception {
return new AnnotationTestBeanFactory();
}