Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke lifecycle methods in AOT mode in the same order as standard JVM mode #30755

Open
sbrannen opened this issue Jun 26, 2023 · 1 comment
Open
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: enhancement A general enhancement
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Jun 26, 2023

Overview

The difference in invocation order between standard JVM mode and AOT mode can be viewed in the following tests. See also: #30724 (comment).

Tests

JVM mode:

@Test
void jakartaAnnotationsCustomPrivateInitDestroyMethodsWithTheSameMethodNames() {
Class<?> beanClass = CustomAnnotatedPrivateSameNameInitDestroyBean.class;
DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass, "customInit", "customDestroy");
CustomAnnotatedPrivateSameNameInitDestroyBean bean = beanFactory.getBean(CustomAnnotatedPrivateSameNameInitDestroyBean.class);
assertThat(bean.initMethods).as("init-methods").containsExactly(
"@PostConstruct.privateCustomInit1",
"@PostConstruct.sameNameCustomInit1",
"afterPropertiesSet",
"customInit"
);
beanFactory.destroySingletons();
assertThat(bean.destroyMethods).as("destroy-methods").containsExactly(
"@PreDestroy.sameNameCustomDestroy1",
"@PreDestroy.privateCustomDestroy1",
"destroy",
"customDestroy"
);
}

AOT mode:

@Test
@CompileWithForkedClassLoader
void jakartaAnnotationsWithCustomSameMethodNamesWithAotProcessingAndAotRuntime() {
Class<CustomAnnotatedPrivateSameNameInitDestroyBean> beanClass = CustomAnnotatedPrivateSameNameInitDestroyBean.class;
GenericApplicationContext applicationContext = new GenericApplicationContext();
DefaultListableBeanFactory beanFactory = applicationContext.getDefaultListableBeanFactory();
AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);
RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass);
beanDefinition.setInitMethodName("customInit");
beanDefinition.setDestroyMethodName("customDestroy");
beanFactory.registerBeanDefinition("lifecycleTestBean", beanDefinition);
testCompiledResult(applicationContext, (initializer, compiled) -> {
GenericApplicationContext aotApplicationContext = createApplicationContext(initializer);
CustomAnnotatedPrivateSameNameInitDestroyBean bean = aotApplicationContext.getBean("lifecycleTestBean", beanClass);
assertThat(bean.initMethods).as("init-methods").containsExactly(
"afterPropertiesSet",
"@PostConstruct.privateCustomInit1",
"@PostConstruct.sameNameCustomInit1",
"customInit"
);
aotApplicationContext.close();
assertThat(bean.destroyMethods).as("destroy-methods").containsExactly(
"destroy",
"@PreDestroy.sameNameCustomDestroy1",
"@PreDestroy.privateCustomDestroy1",
"customDestroy"
);
});
}

Known Issues

  • InitDestroyAnnotationBeanPostProcessor.processAheadOfTime(RegisteredBean) gets invoked twice in the same AOT processing phase: once via CommonAnnotationBeanPostProcessor and once via InitDestroyAnnotationBeanPostProcessor.
  • InitializingBean and DisposableBean are treated specially even in AOT mode.
@sbrannen sbrannen added type: bug A general bug in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing labels Jun 26, 2023
@sbrannen sbrannen added this to the 6.1.0-M2 milestone Jun 26, 2023
@jhoeller jhoeller modified the milestones: 6.1.0-M2, 6.1.x Jul 5, 2023
@sbrannen
Copy link
Member Author

sbrannen commented Jul 9, 2023

Note: the expected order is documented in the Combining Lifecycle Mechanisms section of the reference manual.

@sbrannen sbrannen added type: enhancement A general enhancement and removed type: bug A general bug labels Jul 9, 2023
@sbrannen sbrannen changed the title Init/destroy methods are invoked in different order in AOT mode Invoke lifecycle methods in AOT mode in the same order as standard JVM mode Jul 9, 2023
@jhoeller jhoeller modified the milestones: 6.1.x, 6.x Backlog Nov 9, 2023
bdshadow added a commit to bdshadow/spring-framework that referenced this issue Nov 26, 2023
…onAotProcessor

closes spring-projectsgh-30755

Signed-off-by: Dmitrii Bocharov <bdshadow@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: enhancement A general enhancement
Projects
None yet
2 participants