@ -20,9 +20,13 @@ import java.util.Arrays;
@@ -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.*;
@@ -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 {
@@ -92,40 +97,57 @@ public class ConfigurationClassProcessingTests {
}
@Rule
public final ExpectedException exception = ExpectedException . none ( ) ;
@Test
public void customBeanNameIsRespected ( ) {
public void customBeanNameIsRespectedWhenConfiguredViaNameAttribute ( ) {
customBeanNameIsRespected ( ConfigWithBeanWithCustomName . class ,
( ) - > ConfigWithBeanWithCustomName . testBean , "customName" ) ;
}
@Test
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 ( ConfigWithBeanWithCustomName . class ) ) ;
ac . registerBeanDefinition ( "config" , new RootBeanDefinition ( testC lass) ) ;
ac . refresh ( ) ;
assertSame ( ac . getBean ( "customName" ) , ConfigWithBeanWithCustomName . testBean ) ;
assertSame ( testBeanSupplier . get ( ) , ac . getBean ( beanName ) ) ;
// method name should not be registered
try {
ac . 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 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 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
try {
factory . getBean ( "methodName" ) ;
fail ( "bean should not have been registered with 'methodName'" ) ;
}
catch ( NoSuchBeanDefinitionException ex ) {
// expected
}
exception . expect ( NoSuchBeanDefinitionException . class ) ;
factory . getBean ( "methodName" ) ;
}
@Test // SPR-11830
@ -146,8 +168,9 @@ public class ConfigurationClassProcessingTests {
@@ -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 {
@@ -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 {
@@ -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" } )
public TestBean methodName ( ) {
return testBean ;
}
}
@Configuration
static class ConfigWithBeanWithAliasesConfiguredViaValueAttribute {
static TestBean testBean = new TestBean ( ConfigWithBeanWithAliasesConfiguredViaValueAttribute . class . getSimpleName ( ) ) ;
@Bean ( name = { "name1" , "alias1" , "alias2" , "alias3" } )
@Bean ( { "enigma ", "alias1" , "alias2" , "alias3" } )
public TestBean methodName ( ) {
return testBean ;
}
@ -270,9 +316,9 @@ public class ConfigurationClassProcessingTests {
@@ -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 {
@@ -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 ;
}