Add support of init and destroy methods
This commit updates InitDestroyBeanPostProcessor so that it contributes init or destroy method names to the `RootBeanDefinition`. This is then used by the generator to provide these methods to the optimized AOT context. Invocation of those init methods still happen using reflection so dedicated hints are contributed for them. Closes gh-28151
This commit is contained in:
@@ -43,9 +43,11 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.annotation.AnnotationConfigUtils;
|
||||
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.context.testfixture.context.generator.SimpleComponent;
|
||||
import org.springframework.context.testfixture.context.generator.annotation.AutowiredComponent;
|
||||
import org.springframework.context.testfixture.context.generator.annotation.InitDestroyComponent;
|
||||
import org.springframework.javapoet.ClassName;
|
||||
import org.springframework.javapoet.CodeBlock;
|
||||
import org.springframework.javapoet.JavaFile;
|
||||
@@ -94,6 +96,41 @@ class ApplicationContextAotGeneratorTests {
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateApplicationContextWithInitDestroyMethods() {
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME,
|
||||
BeanDefinitionBuilder.rootBeanDefinition(CommonAnnotationBeanPostProcessor.class)
|
||||
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition());
|
||||
context.registerBeanDefinition("initDestroyComponent", new RootBeanDefinition(InitDestroyComponent.class));
|
||||
compile(context, toFreshApplicationContext(GenericApplicationContext::new, aotContext -> {
|
||||
assertThat(aotContext.getBeanDefinitionNames()).containsOnly("initDestroyComponent");
|
||||
InitDestroyComponent bean = aotContext.getBean(InitDestroyComponent.class);
|
||||
assertThat(bean.events).containsExactly("init");
|
||||
aotContext.close();
|
||||
assertThat(bean.events).containsExactly("init", "destroy");
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateApplicationContextWithMultipleInitDestroyMethods() {
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBeanDefinition(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME,
|
||||
BeanDefinitionBuilder.rootBeanDefinition(CommonAnnotationBeanPostProcessor.class)
|
||||
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition());
|
||||
RootBeanDefinition beanDefinition = new RootBeanDefinition(InitDestroyComponent.class);
|
||||
beanDefinition.setInitMethodName("customInit");
|
||||
beanDefinition.setDestroyMethodName("customDestroy");
|
||||
context.registerBeanDefinition("initDestroyComponent", beanDefinition);
|
||||
compile(context, toFreshApplicationContext(GenericApplicationContext::new, aotContext -> {
|
||||
assertThat(aotContext.getBeanDefinitionNames()).containsOnly("initDestroyComponent");
|
||||
InitDestroyComponent bean = aotContext.getBean(InitDestroyComponent.class);
|
||||
assertThat(bean.events).containsExactly("customInit", "init");
|
||||
aotContext.close();
|
||||
assertThat(bean.events).containsExactly("customInit", "init", "customDestroy", "destroy");
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateApplicationContextWitNoContributors() {
|
||||
GeneratedTypeContext generationContext = createGenerationContext();
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.testfixture.context.generator.annotation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
|
||||
public class InitDestroyComponent {
|
||||
|
||||
public final List<String> events = new ArrayList<>();
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.events.add("init");
|
||||
}
|
||||
|
||||
public void customInit() {
|
||||
this.events.add("customInit");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
this.events.add("destroy");
|
||||
}
|
||||
|
||||
public void customDestroy() {
|
||||
this.events.add("customDestroy");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user