Skip to content

Commit 9a9c3ea

Browse files
committed
Filter single inferred init/destroy methods
Update `BeanDefinitionPropertiesCodeGenerator` to not add init/destroy method calls for a single inferred name. Closes gh-28570
1 parent 172102d commit 9a9c3ea

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGenerator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ private void addInitDestroyMethods(Builder builder,
151151
addInitDestroyHint(beanType, methodName);
152152
}
153153
}
154-
builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments.build());
154+
if (!arguments.isEmpty()) {
155+
builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments.build());
156+
}
155157
}
156158
}
157159

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGeneratorTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
3838
import org.springframework.beans.factory.config.RuntimeBeanNameReference;
3939
import org.springframework.beans.factory.config.RuntimeBeanReference;
40+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
4041
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
4142
import org.springframework.beans.factory.support.ManagedList;
4243
import org.springframework.beans.factory.support.ManagedMap;
@@ -229,6 +230,13 @@ void setInitMethodWhenSingleInitMethod() {
229230
assertHasMethodInvokeHints(InitDestroyBean.class, methodNames);
230231
}
231232

233+
@Test
234+
void setInitMethodWhenSingleInferredInitMethod() {
235+
this.beanDefinition.setTargetType(InitDestroyBean.class);
236+
this.beanDefinition.setInitMethodName(AbstractBeanDefinition.INFER_METHOD);
237+
testCompiledResult((actual, compiled) -> assertThat(actual.getInitMethodNames()).isNull());
238+
}
239+
232240
@Test
233241
void setInitMethodWhenMultipleInitMethods() {
234242
this.beanDefinition.setTargetType(InitDestroyBean.class);
@@ -250,6 +258,13 @@ void setDestroyMethodWhenDestroyInitMethod() {
250258
assertHasMethodInvokeHints(InitDestroyBean.class, methodNames);
251259
}
252260

261+
@Test
262+
void setDestroyMethodWhenSingleInferredInitMethod() {
263+
this.beanDefinition.setTargetType(InitDestroyBean.class);
264+
this.beanDefinition.setDestroyMethodName(AbstractBeanDefinition.INFER_METHOD);
265+
testCompiledResult((actual, compiled) -> assertThat(actual.getDestroyMethodNames()).isNull());
266+
}
267+
253268
@Test
254269
void setDestroyMethodWhenMultipleDestroyMethods() {
255270
this.beanDefinition.setTargetType(InitDestroyBean.class);

0 commit comments

Comments
 (0)