Skip to content

Commit 044258f

Browse files
committed
Support abstract @⁠Configuration classes without @⁠Bean methods again
Historically, @⁠Configuration classes that did not declare @⁠Bean methods were allowed to be abstract. However, the changes made in 76a6b9ea79 introduced a regression that prevents such classes from being abstract, resulting in a BeanInstantiationException. This change in behavior is caused by the fact that such a @⁠Configuration class is no longer replaced by a concrete subclass created dynamically by CGLIB. This commit restores support for abstract @⁠Configuration classes without @⁠Bean methods by modifying the "no enhancement required" check in ConfigurationClassParser. See gh-34486 Closes gh-34663
1 parent 36d9357 commit 044258f

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ else if (bd instanceof AbstractBeanDefinition abstractBeanDef && abstractBeanDef
179179
}
180180

181181
// Downgrade to lite (no enhancement) in case of no instance-level @Bean methods.
182-
if (!configClass.hasNonStaticBeanMethods() && ConfigurationClassUtils.CONFIGURATION_CLASS_FULL.equals(
183-
bd.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE))) {
182+
if (!configClass.getMetadata().isAbstract() && !configClass.hasNonStaticBeanMethods() &&
183+
ConfigurationClassUtils.CONFIGURATION_CLASS_FULL.equals(
184+
bd.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE))) {
184185
bd.setAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE,
185186
ConfigurationClassUtils.CONFIGURATION_CLASS_LITE);
186187
}

spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ void enhancementIsPresentBecauseSingletonSemanticsAreRespectedUsingAsm() {
129129
assertThat(beanFactory.getDependentBeans("config")).contains("bar");
130130
}
131131

132+
@Test // gh-34663
133+
void enhancementIsPresentForAbstractConfigClassWithoutBeanMethods() {
134+
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(AbstractConfigWithoutBeanMethods.class));
135+
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
136+
pp.postProcessBeanFactory(beanFactory);
137+
RootBeanDefinition beanDefinition = (RootBeanDefinition) beanFactory.getBeanDefinition("config");
138+
assertThat(beanDefinition.hasBeanClass()).isTrue();
139+
assertThat(beanDefinition.getBeanClass().getName()).contains(ClassUtils.CGLIB_CLASS_SEPARATOR);
140+
Foo foo = beanFactory.getBean("foo", Foo.class);
141+
Bar bar = beanFactory.getBean("bar", Bar.class);
142+
assertThat(bar.foo).isSameAs(foo);
143+
assertThat(beanFactory.getDependentBeans("foo")).contains("bar");
144+
String[] dependentsOfSingletonBeanConfig = beanFactory.getDependentBeans(SingletonBeanConfig.class.getName());
145+
assertThat(dependentsOfSingletonBeanConfig).containsOnly("foo", "bar");
146+
}
147+
132148
@Test
133149
void enhancementIsNotPresentForProxyBeanMethodsFlagSetToFalse() {
134150
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(NonEnhancedSingletonBeanConfig.class));
@@ -181,7 +197,7 @@ void enhancementIsNotPresentForStaticMethodsUsingAsm() {
181197
assertThat(bar.foo).isNotSameAs(foo);
182198
}
183199

184-
@Test
200+
@Test // gh-34486
185201
void enhancementIsNotPresentWithEmptyConfig() {
186202
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(EmptyConfig.class));
187203
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
@@ -1195,6 +1211,12 @@ public static Bar bar() {
11951211
}
11961212
}
11971213

1214+
@Configuration
1215+
@Import(SingletonBeanConfig.class)
1216+
abstract static class AbstractConfigWithoutBeanMethods {
1217+
// This class intentionally does NOT declare @Bean methods.
1218+
}
1219+
11981220
@Configuration
11991221
static final class EmptyConfig {
12001222
}

0 commit comments

Comments
 (0)