Merge branch '6.1.x'

# Conflicts:
#	spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClass.java
This commit is contained in:
Juergen Hoeller 2024-02-21 23:01:09 +01:00
commit 491ebb4ec4
4 changed files with 86 additions and 39 deletions

View File

@ -611,13 +611,10 @@ class ConstructorResolver {
String argDesc = StringUtils.collectionToCommaDelimitedString(argTypes); String argDesc = StringUtils.collectionToCommaDelimitedString(argTypes);
throw new BeanCreationException(mbd.getResourceDescription(), beanName, throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"No matching factory method found on class [" + factoryClass.getName() + "]: " + "No matching factory method found on class [" + factoryClass.getName() + "]: " +
(mbd.getFactoryBeanName() != null ? (mbd.getFactoryBeanName() != null ? "factory bean '" + mbd.getFactoryBeanName() + "'; " : "") +
"factory bean '" + mbd.getFactoryBeanName() + "'; " : "") +
"factory method '" + mbd.getFactoryMethodName() + "(" + argDesc + ")'. " + "factory method '" + mbd.getFactoryMethodName() + "(" + argDesc + ")'. " +
"Check that a method with the specified name " + "Check that a method with the specified name " + (minNrOfArgs > 0 ? "and arguments " : "") +
(minNrOfArgs > 0 ? "and arguments " : "") + "exists and that it is " + (isStatic ? "static" : "non-static") + ".");
"exists and that it is " +
(isStatic ? "static" : "non-static") + ".");
} }
else if (void.class == factoryMethodToUse.getReturnType()) { else if (void.class == factoryMethodToUse.getReturnType()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, throw new BeanCreationException(mbd.getResourceDescription(), beanName,
@ -1197,8 +1194,7 @@ class ConstructorResolver {
} }
private Predicate<ResolvableType> isAssignable(ResolvableType valueType) { private Predicate<ResolvableType> isAssignable(ResolvableType valueType) {
return parameterType -> (valueType == ResolvableType.NONE return parameterType -> (valueType == ResolvableType.NONE || parameterType.isAssignableFrom(valueType));
|| parameterType.isAssignableFrom(valueType));
} }
private ResolvableType extractElementType(ResolvableType parameterType) { private ResolvableType extractElementType(ResolvableType parameterType) {

View File

@ -613,16 +613,16 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
*/ */
protected void destroyBean(String beanName, @Nullable DisposableBean bean) { protected void destroyBean(String beanName, @Nullable DisposableBean bean) {
// Trigger destruction of dependent beans first... // Trigger destruction of dependent beans first...
Set<String> dependencies; Set<String> dependentBeanNames;
synchronized (this.dependentBeanMap) { synchronized (this.dependentBeanMap) {
// Within full synchronization in order to guarantee a disconnected Set // Within full synchronization in order to guarantee a disconnected Set
dependencies = this.dependentBeanMap.remove(beanName); dependentBeanNames = this.dependentBeanMap.remove(beanName);
} }
if (dependencies != null) { if (dependentBeanNames != null) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Retrieved dependent beans for bean '" + beanName + "': " + dependencies); logger.trace("Retrieved dependent beans for bean '" + beanName + "': " + dependentBeanNames);
} }
for (String dependentBeanName : dependencies) { for (String dependentBeanName : dependentBeanNames) {
destroySingleton(dependentBeanName); destroySingleton(dependentBeanName);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -80,15 +80,16 @@ class ComponentScanAnnotationIntegrationTests {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan(example.scannable.PackageMarker.class.getPackage().getName()); ctx.scan(example.scannable.PackageMarker.class.getPackage().getName());
ctx.refresh(); ctx.refresh();
assertContextContainsBean(ctx, "fooServiceImpl"); assertContextContainsBean(ctx, "fooServiceImpl");
} }
@Test @Test
void viaContextRegistration() { void viaContextRegistration() {
AnnotationConfigApplicationContext ctx = ApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfig.class);
new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfig.class);
ctx.getBean(ComponentScanAnnotatedConfig.class); ctx.getBean(ComponentScanAnnotatedConfig.class);
ctx.getBean(TestBean.class); ctx.getBean(TestBean.class);
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found").isTrue(); assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found").isTrue();
assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered directly against " + assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered directly against " +
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue(); "AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
@ -96,10 +97,10 @@ class ComponentScanAnnotationIntegrationTests {
@Test @Test
void viaContextRegistration_WithValueAttribute() { void viaContextRegistration_WithValueAttribute() {
AnnotationConfigApplicationContext ctx = ApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfig_WithValueAttribute.class);
new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfig_WithValueAttribute.class);
ctx.getBean(ComponentScanAnnotatedConfig_WithValueAttribute.class); ctx.getBean(ComponentScanAnnotatedConfig_WithValueAttribute.class);
ctx.getBean(TestBean.class); ctx.getBean(TestBean.class);
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig_WithValueAttribute")).as("config class bean not found").isTrue(); assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig_WithValueAttribute")).as("config class bean not found").isTrue();
assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered directly against " + assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered directly against " +
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue(); "AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
@ -107,9 +108,9 @@ class ComponentScanAnnotationIntegrationTests {
@Test @Test
void viaContextRegistration_FromPackageOfConfigClass() { void viaContextRegistration_FromPackageOfConfigClass() {
AnnotationConfigApplicationContext ctx = ApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfigWithImplicitBasePackage.class);
new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfigWithImplicitBasePackage.class);
ctx.getBean(ComponentScanAnnotatedConfigWithImplicitBasePackage.class); ctx.getBean(ComponentScanAnnotatedConfigWithImplicitBasePackage.class);
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfigWithImplicitBasePackage")).as("config class bean not found").isTrue(); assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfigWithImplicitBasePackage")).as("config class bean not found").isTrue();
assertThat(ctx.containsBean("scannedComponent")).as("@ComponentScan annotated @Configuration class registered directly against " + assertThat(ctx.containsBean("scannedComponent")).as("@ComponentScan annotated @Configuration class registered directly against " +
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue(); "AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
@ -118,11 +119,12 @@ class ComponentScanAnnotationIntegrationTests {
@Test @Test
void viaContextRegistration_WithComposedAnnotation() { void viaContextRegistration_WithComposedAnnotation() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComposedAnnotationConfig.class); ApplicationContext ctx = new AnnotationConfigApplicationContext(ComposedAnnotationConfig.class);
ctx.getBean(ComposedAnnotationConfig.class); ctx.getBean(ComposedAnnotationConfig.class);
ctx.getBean(SimpleComponent.class); ctx.getBean(SimpleComponent.class);
ctx.getBean(ClassWithNestedComponents.NestedComponent.class); ctx.getBean(ClassWithNestedComponents.NestedComponent.class);
ctx.getBean(ClassWithNestedComponents.OtherNestedComponent.class); ctx.getBean(ClassWithNestedComponents.OtherNestedComponent.class);
assertThat(ctx.containsBeanDefinition("componentScanAnnotationIntegrationTests.ComposedAnnotationConfig")).as("config class bean not found").isTrue(); assertThat(ctx.containsBeanDefinition("componentScanAnnotationIntegrationTests.ComposedAnnotationConfig")).as("config class bean not found").isTrue();
assertThat(ctx.containsBean("simpleComponent")).as("@ComponentScan annotated @Configuration class registered directly against " + assertThat(ctx.containsBean("simpleComponent")).as("@ComponentScan annotated @Configuration class registered directly against " +
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue(); "AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
@ -132,6 +134,7 @@ class ComponentScanAnnotationIntegrationTests {
void multipleComposedComponentScanAnnotations() { // gh-30941 void multipleComposedComponentScanAnnotations() { // gh-30941
ApplicationContext ctx = new AnnotationConfigApplicationContext(MultipleComposedAnnotationsConfig.class); ApplicationContext ctx = new AnnotationConfigApplicationContext(MultipleComposedAnnotationsConfig.class);
ctx.getBean(MultipleComposedAnnotationsConfig.class); ctx.getBean(MultipleComposedAnnotationsConfig.class);
assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.MultipleComposedAnnotationsConfig"); assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.MultipleComposedAnnotationsConfig");
assertContextContainsBean(ctx, "simpleComponent"); assertContextContainsBean(ctx, "simpleComponent");
assertContextContainsBean(ctx, "barComponent"); assertContextContainsBean(ctx, "barComponent");
@ -143,7 +146,6 @@ class ComponentScanAnnotationIntegrationTests {
assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.LocalAnnotationOverridesMultipleMetaAnnotationsConfig"); assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.LocalAnnotationOverridesMultipleMetaAnnotationsConfig");
assertContextContainsBean(ctx, "barComponent"); assertContextContainsBean(ctx, "barComponent");
assertContextDoesNotContainBean(ctx, "simpleComponent"); assertContextDoesNotContainBean(ctx, "simpleComponent");
assertContextDoesNotContainBean(ctx, "configurableComponent"); assertContextDoesNotContainBean(ctx, "configurableComponent");
} }
@ -154,7 +156,6 @@ class ComponentScanAnnotationIntegrationTests {
assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.LocalAnnotationOverridesMultipleComposedAnnotationsConfig"); assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.LocalAnnotationOverridesMultipleComposedAnnotationsConfig");
assertContextContainsBean(ctx, "barComponent"); assertContextContainsBean(ctx, "barComponent");
assertContextDoesNotContainBean(ctx, "simpleComponent"); assertContextDoesNotContainBean(ctx, "simpleComponent");
assertContextDoesNotContainBean(ctx, "configurableComponent"); assertContextDoesNotContainBean(ctx, "configurableComponent");
} }
@ -166,7 +167,6 @@ class ComponentScanAnnotationIntegrationTests {
assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.LocalRepeatedAnnotationsOverrideComposedAnnotationsConfig"); assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.LocalRepeatedAnnotationsOverrideComposedAnnotationsConfig");
assertContextContainsBean(ctx, "barComponent"); assertContextContainsBean(ctx, "barComponent");
assertContextContainsBean(ctx, "configurableComponent"); assertContextContainsBean(ctx, "configurableComponent");
assertContextDoesNotContainBean(ctx, "simpleComponent"); assertContextDoesNotContainBean(ctx, "simpleComponent");
} }
@ -177,7 +177,6 @@ class ComponentScanAnnotationIntegrationTests {
assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.LocalRepeatedAnnotationsInContainerOverrideComposedAnnotationsConfig"); assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.LocalRepeatedAnnotationsInContainerOverrideComposedAnnotationsConfig");
assertContextContainsBean(ctx, "barComponent"); assertContextContainsBean(ctx, "barComponent");
assertContextContainsBean(ctx, "configurableComponent"); assertContextContainsBean(ctx, "configurableComponent");
assertContextDoesNotContainBean(ctx, "simpleComponent"); assertContextDoesNotContainBean(ctx, "simpleComponent");
} }
@ -188,10 +187,12 @@ class ComponentScanAnnotationIntegrationTests {
genericBeanDefinition(ComponentScanAnnotatedConfig.class).getBeanDefinition()); genericBeanDefinition(ComponentScanAnnotatedConfig.class).getBeanDefinition());
bf.registerBeanDefinition("configurationClassPostProcessor", bf.registerBeanDefinition("configurationClassPostProcessor",
genericBeanDefinition(ConfigurationClassPostProcessor.class).getBeanDefinition()); genericBeanDefinition(ConfigurationClassPostProcessor.class).getBeanDefinition());
GenericApplicationContext ctx = new GenericApplicationContext(bf); GenericApplicationContext ctx = new GenericApplicationContext(bf);
ctx.refresh(); ctx.refresh();
ctx.getBean(ComponentScanAnnotatedConfig.class); ctx.getBean(ComponentScanAnnotatedConfig.class);
ctx.getBean(TestBean.class); ctx.getBean(TestBean.class);
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found").isTrue(); assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found").isTrue();
assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered as bean " + assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered as bean " +
"definition did not trigger component scanning as expected").isTrue(); "definition did not trigger component scanning as expected").isTrue();
@ -206,7 +207,8 @@ class ComponentScanAnnotationIntegrationTests {
@Test @Test
void withScopeResolver() { void withScopeResolver() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithScopeResolver.class); ApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithScopeResolver.class);
// custom scope annotation makes the bean prototype scoped. subsequent calls // custom scope annotation makes the bean prototype scoped. subsequent calls
// to getBean should return distinct instances. // to getBean should return distinct instances.
assertThat(ctx.getBean(CustomScopeAnnotationBean.class)).isNotSameAs(ctx.getBean(CustomScopeAnnotationBean.class)); assertThat(ctx.getBean(CustomScopeAnnotationBean.class)).isNotSameAs(ctx.getBean(CustomScopeAnnotationBean.class));
@ -215,7 +217,8 @@ class ComponentScanAnnotationIntegrationTests {
@Test @Test
void multiComponentScan() { void multiComponentScan() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MultiComponentScan.class); ApplicationContext ctx = new AnnotationConfigApplicationContext(MultiComponentScan.class);
assertThat(ctx.getBean(CustomScopeAnnotationBean.class)).isNotSameAs(ctx.getBean(CustomScopeAnnotationBean.class)); assertThat(ctx.getBean(CustomScopeAnnotationBean.class)).isNotSameAs(ctx.getBean(CustomScopeAnnotationBean.class));
assertContextContainsBean(ctx, "scannedComponent"); assertContextContainsBean(ctx, "scannedComponent");
} }
@ -223,14 +226,16 @@ class ComponentScanAnnotationIntegrationTests {
@Test @Test
void withCustomTypeFilter() { void withCustomTypeFilter() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithCustomTypeFilter.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithCustomTypeFilter.class);
assertThat(ctx.getDefaultListableBeanFactory().containsSingleton("componentScanParserTests.KustomAnnotationAutowiredBean")).isFalse();
assertThat(ctx.getBeanFactory().containsSingleton("componentScanParserTests.KustomAnnotationAutowiredBean")).isFalse();
KustomAnnotationAutowiredBean testBean = ctx.getBean("componentScanParserTests.KustomAnnotationAutowiredBean", KustomAnnotationAutowiredBean.class); KustomAnnotationAutowiredBean testBean = ctx.getBean("componentScanParserTests.KustomAnnotationAutowiredBean", KustomAnnotationAutowiredBean.class);
assertThat(testBean.getDependency()).isNotNull(); assertThat(testBean.getDependency()).isNotNull();
} }
@Test @Test
void withAwareTypeFilter() { void withAwareTypeFilter() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithAwareTypeFilter.class); ApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithAwareTypeFilter.class);
assertThat(ctx.getEnvironment().matchesProfiles("the-filter-ran")).isTrue(); assertThat(ctx.getEnvironment().matchesProfiles("the-filter-ran")).isTrue();
} }
@ -240,6 +245,7 @@ class ComponentScanAnnotationIntegrationTests {
ctx.register(ComponentScanWithScopedProxy.class); ctx.register(ComponentScanWithScopedProxy.class);
ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
ctx.refresh(); ctx.refresh();
// should cast to the interface // should cast to the interface
FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
// should be dynamic proxy // should be dynamic proxy
@ -257,6 +263,7 @@ class ComponentScanAnnotationIntegrationTests {
ctx.register(ComponentScanWithScopedProxyThroughRegex.class); ctx.register(ComponentScanWithScopedProxyThroughRegex.class);
ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
ctx.refresh(); ctx.refresh();
// should cast to the interface // should cast to the interface
FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
// should be dynamic proxy // should be dynamic proxy
@ -269,6 +276,7 @@ class ComponentScanAnnotationIntegrationTests {
ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class); ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class);
ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
ctx.refresh(); ctx.refresh();
// should cast to the interface // should cast to the interface
FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
// should be dynamic proxy // should be dynamic proxy
@ -277,16 +285,16 @@ class ComponentScanAnnotationIntegrationTests {
@Test @Test
void withMultipleAnnotationIncludeFilters1() { void withMultipleAnnotationIncludeFilters1() {
AnnotationConfigApplicationContext ctx = ApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithMultipleAnnotationIncludeFilters1.class);
new AnnotationConfigApplicationContext(ComponentScanWithMultipleAnnotationIncludeFilters1.class);
ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
ctx.getBean(MessageBean.class); // @CustomComponent-annotated ctx.getBean(MessageBean.class); // @CustomComponent-annotated
} }
@Test @Test
void withMultipleAnnotationIncludeFilters2() { void withMultipleAnnotationIncludeFilters2() {
AnnotationConfigApplicationContext ctx = ApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithMultipleAnnotationIncludeFilters2.class);
new AnnotationConfigApplicationContext(ComponentScanWithMultipleAnnotationIncludeFilters2.class);
ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
ctx.getBean(MessageBean.class); // @CustomComponent-annotated ctx.getBean(MessageBean.class); // @CustomComponent-annotated
} }
@ -296,25 +304,28 @@ class ComponentScanAnnotationIntegrationTests {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters3.class); ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters3.class);
ctx.refresh(); ctx.refresh();
assertThat(ctx.getBean(DefaultNamedComponent.class).toString()).isEqualTo("overridden"); assertThat(ctx.getBean(DefaultNamedComponent.class).toString()).isEqualTo("overridden");
} }
@Test @Test
void withBeanMethodOverrideAndGeneralOverridingDisabled() { void withBeanMethodOverrideAndGeneralOverridingDisabled() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getDefaultListableBeanFactory().setAllowBeanDefinitionOverriding(false); ctx.setAllowBeanDefinitionOverriding(false);
ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters3.class); ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters3.class);
ctx.refresh(); ctx.refresh();
assertThat(ctx.getBean(DefaultNamedComponent.class).toString()).isEqualTo("overridden"); assertThat(ctx.getBean(DefaultNamedComponent.class).toString()).isEqualTo("overridden");
} }
@Test @Test
void withBasePackagesAndValueAlias() { void withBasePackagesAndValueAlias() {
AnnotationConfigApplicationContext ctx = ApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithBasePackagesAndValueAlias.class);
new AnnotationConfigApplicationContext(ComponentScanWithBasePackagesAndValueAlias.class);
assertContextContainsBean(ctx, "fooServiceImpl"); assertContextContainsBean(ctx, "fooServiceImpl");
} }
private static void assertContextContainsBean(ApplicationContext ctx, String beanName) { private static void assertContextContainsBean(ApplicationContext ctx, String beanName) {
assertThat(ctx.containsBean(beanName)).as("context should contain bean " + beanName).isTrue(); assertThat(ctx.containsBean(beanName)).as("context should contain bean " + beanName).isTrue();
} }

View File

@ -35,6 +35,8 @@ import org.springframework.aop.support.AopUtils;
import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
@ -944,8 +946,8 @@ class ConfigurationClassPostProcessorTests {
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ConcreteConfig.class)); beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ConcreteConfig.class));
beanFactory.registerBeanDefinition("serviceBeanProvider", new RootBeanDefinition(ServiceBeanProvider.class)); beanFactory.registerBeanDefinition("serviceBeanProvider", new RootBeanDefinition(ServiceBeanProvider.class));
new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
beanFactory.preInstantiateSingletons();
beanFactory.preInstantiateSingletons();
beanFactory.getBean(ServiceBean.class); beanFactory.getBean(ServiceBean.class);
} }
@ -958,8 +960,8 @@ class ConfigurationClassPostProcessorTests {
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ConcreteConfigWithDefaultMethods.class)); beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ConcreteConfigWithDefaultMethods.class));
beanFactory.registerBeanDefinition("serviceBeanProvider", new RootBeanDefinition(ServiceBeanProvider.class)); beanFactory.registerBeanDefinition("serviceBeanProvider", new RootBeanDefinition(ServiceBeanProvider.class));
new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
beanFactory.preInstantiateSingletons();
beanFactory.preInstantiateSingletons();
beanFactory.getBean(ServiceBean.class); beanFactory.getBean(ServiceBean.class);
} }
@ -972,11 +974,25 @@ class ConfigurationClassPostProcessorTests {
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ConcreteConfigWithDefaultMethods.class.getName())); beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ConcreteConfigWithDefaultMethods.class.getName()));
beanFactory.registerBeanDefinition("serviceBeanProvider", new RootBeanDefinition(ServiceBeanProvider.class.getName())); beanFactory.registerBeanDefinition("serviceBeanProvider", new RootBeanDefinition(ServiceBeanProvider.class.getName()));
new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
beanFactory.preInstantiateSingletons();
beanFactory.preInstantiateSingletons();
beanFactory.getBean(ServiceBean.class); beanFactory.getBean(ServiceBean.class);
} }
@Test
void testConfigWithFailingInit() { // gh-23343
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(beanFactory);
beanFactory.addBeanPostProcessor(bpp);
beanFactory.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ConcreteConfigWithFailingInit.class));
new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(beanFactory::preInstantiateSingletons);
assertThat(beanFactory.containsSingleton("configClass")).isFalse();
assertThat(beanFactory.containsSingleton("provider")).isFalse();
}
@Test @Test
void testCircularDependency() { void testCircularDependency() {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
@ -985,6 +1001,7 @@ class ConfigurationClassPostProcessorTests {
beanFactory.registerBeanDefinition("configClass1", new RootBeanDefinition(A.class)); beanFactory.registerBeanDefinition("configClass1", new RootBeanDefinition(A.class));
beanFactory.registerBeanDefinition("configClass2", new RootBeanDefinition(AStrich.class)); beanFactory.registerBeanDefinition("configClass2", new RootBeanDefinition(AStrich.class));
new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
assertThatExceptionOfType(BeanCreationException.class) assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(beanFactory::preInstantiateSingletons) .isThrownBy(beanFactory::preInstantiateSingletons)
.withMessageContaining("Circular reference"); .withMessageContaining("Circular reference");
@ -1768,6 +1785,29 @@ class ConfigurationClassPostProcessorTests {
} }
} }
@Configuration
public static class ConcreteConfigWithFailingInit implements DefaultMethodsConfig, BeanFactoryAware {
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Bean
@Override
public ServiceBeanProvider provider() {
return new ServiceBeanProvider();
}
@PostConstruct
public void validate() {
beanFactory.getBean("provider");
throw new IllegalStateException();
}
}
@Primary @Primary
public static class ServiceBeanProvider { public static class ServiceBeanProvider {