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:
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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.beans.factory.annotation;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.generator.BeanInstantiationContribution;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.testfixture.beans.factory.generator.lifecycle.Destroy;
|
||||
import org.springframework.beans.testfixture.beans.factory.generator.lifecycle.Init;
|
||||
import org.springframework.beans.testfixture.beans.factory.generator.lifecycle.InitDestroyBean;
|
||||
import org.springframework.beans.testfixture.beans.factory.generator.lifecycle.MultiInitDestroyBean;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
|
||||
/**
|
||||
* Tests for {@link InitDestroyAnnotationBeanPostProcessor}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class InitDestroyAnnotationBeanPostProcessorTests {
|
||||
|
||||
@Test
|
||||
void contributeWithNoCallbackDoesNotMutateRootBeanDefinition() {
|
||||
RootBeanDefinition beanDefinition = mock(RootBeanDefinition.class);
|
||||
assertThat(createAotContributingBeanPostProcessor().contribute(
|
||||
beanDefinition, String.class, "test")).isNull();
|
||||
verifyNoInteractions(beanDefinition);
|
||||
}
|
||||
|
||||
@Test
|
||||
void contributeWithInitDestroyCallback() {
|
||||
RootBeanDefinition beanDefinition = new RootBeanDefinition(InitDestroyBean.class);
|
||||
assertThat(createContribution(beanDefinition)).isNull();
|
||||
assertThat(beanDefinition.getInitMethodNames()).containsExactly("initMethod");
|
||||
assertThat(beanDefinition.getDestroyMethodNames()).containsExactly("destroyMethod");
|
||||
}
|
||||
|
||||
@Test
|
||||
void contributeWithInitDestroyCallbackRetainCustomMethods() {
|
||||
RootBeanDefinition beanDefinition = new RootBeanDefinition(InitDestroyBean.class);
|
||||
beanDefinition.setInitMethodName("customInitMethod");
|
||||
beanDefinition.setDestroyMethodNames("customDestroyMethod");
|
||||
assertThat(createContribution(beanDefinition)).isNull();
|
||||
assertThat(beanDefinition.getInitMethodNames())
|
||||
.containsExactly("customInitMethod", "initMethod");
|
||||
assertThat(beanDefinition.getDestroyMethodNames())
|
||||
.containsExactly("customDestroyMethod", "destroyMethod");
|
||||
}
|
||||
|
||||
@Test
|
||||
void contributeWithInitDestroyCallbackFilterDuplicates() {
|
||||
RootBeanDefinition beanDefinition = new RootBeanDefinition(InitDestroyBean.class);
|
||||
beanDefinition.setInitMethodName("initMethod");
|
||||
beanDefinition.setDestroyMethodNames("destroyMethod");
|
||||
assertThat(createContribution(beanDefinition)).isNull();
|
||||
assertThat(beanDefinition.getInitMethodNames()).containsExactly("initMethod");
|
||||
assertThat(beanDefinition.getDestroyMethodNames()).containsExactly("destroyMethod");
|
||||
}
|
||||
|
||||
@Test
|
||||
void contributeWithMultipleInitDestroyCallbacks() {
|
||||
RootBeanDefinition beanDefinition = new RootBeanDefinition(MultiInitDestroyBean.class);
|
||||
assertThat(createContribution(beanDefinition)).isNull();
|
||||
assertThat(beanDefinition.getInitMethodNames())
|
||||
.containsExactly("initMethod", "anotherInitMethod");
|
||||
assertThat(beanDefinition.getDestroyMethodNames())
|
||||
.containsExactly("anotherDestroyMethod", "destroyMethod");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private BeanInstantiationContribution createContribution(RootBeanDefinition beanDefinition) {
|
||||
InitDestroyAnnotationBeanPostProcessor bpp = createAotContributingBeanPostProcessor();
|
||||
return bpp.contribute(beanDefinition, beanDefinition.getResolvableType().toClass(), "test");
|
||||
}
|
||||
|
||||
private InitDestroyAnnotationBeanPostProcessor createAotContributingBeanPostProcessor() {
|
||||
InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor();
|
||||
bpp.setInitAnnotationType(Init.class);
|
||||
bpp.setDestroyAnnotationType(Destroy.class);
|
||||
return bpp;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -56,6 +56,7 @@ import org.springframework.beans.testfixture.beans.factory.generator.InnerCompon
|
||||
import org.springframework.beans.testfixture.beans.factory.generator.SimpleConfiguration;
|
||||
import org.springframework.beans.testfixture.beans.factory.generator.factory.SampleFactory;
|
||||
import org.springframework.beans.testfixture.beans.factory.generator.injection.InjectionComponent;
|
||||
import org.springframework.beans.testfixture.beans.factory.generator.lifecycle.InitDestroyBean;
|
||||
import org.springframework.beans.testfixture.beans.factory.generator.property.ConfigurableBean;
|
||||
import org.springframework.beans.testfixture.beans.factory.generator.visibility.ProtectedConstructorComponent;
|
||||
import org.springframework.beans.testfixture.beans.factory.generator.visibility.ProtectedFactoryMethod;
|
||||
@@ -208,6 +209,30 @@ class BeanRegistrationBeanFactoryContributionTests {
|
||||
PublicFactoryBean.class.getPackageName() + ".Test.registerTest(beanFactory);\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateWithBeanDefinitionHavingInitMethodName() {
|
||||
compile(simpleConfigurationRegistration(bd -> bd.setInitMethodName("someMethod")),
|
||||
hasBeanDefinition(generatedBd -> assertThat(generatedBd.getInitMethodNames()).containsExactly("someMethod")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateWithBeanDefinitionHavingInitMethodNames() {
|
||||
compile(simpleConfigurationRegistration(bd -> bd.setInitMethodNames("i1", "i2")),
|
||||
hasBeanDefinition(generatedBd -> assertThat(generatedBd.getInitMethodNames()).containsExactly("i1", "i2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateWithBeanDefinitionHavingDestroyMethodName() {
|
||||
compile(simpleConfigurationRegistration(bd -> bd.setDestroyMethodName("someMethod")),
|
||||
hasBeanDefinition(generatedBd -> assertThat(generatedBd.getDestroyMethodNames()).containsExactly("someMethod")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateWithBeanDefinitionHavingDestroyMethodNames() {
|
||||
compile(simpleConfigurationRegistration(bd -> bd.setDestroyMethodNames("d1", "d2")),
|
||||
hasBeanDefinition(generatedBd -> assertThat(generatedBd.getDestroyMethodNames()).containsExactly("d1", "d2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateWithBeanDefinitionHavingSyntheticFlag() {
|
||||
compile(simpleConfigurationRegistration(bd -> bd.setSynthetic(true)),
|
||||
@@ -392,6 +417,28 @@ class BeanRegistrationBeanFactoryContributionTests {
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerRuntimeHintsWithInitMethodNames() {
|
||||
RootBeanDefinition bd = new RootBeanDefinition(InitDestroyBean.class);
|
||||
bd.setInitMethodNames("customInitMethod", "initMethod");
|
||||
RuntimeHints runtimeHints = new RuntimeHints();
|
||||
getDefaultContribution(new DefaultListableBeanFactory(), bd).registerRuntimeHints(runtimeHints);
|
||||
assertThat(runtimeHints.reflection().getTypeHint(InitDestroyBean.class)).satisfies(hint ->
|
||||
assertThat(hint.methods()).anySatisfy(invokeMethodHint("customInitMethod"))
|
||||
.anySatisfy(invokeMethodHint("initMethod")).hasSize(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerRuntimeHintsWithDestroyMethodNames() {
|
||||
RootBeanDefinition bd = new RootBeanDefinition(InitDestroyBean.class);
|
||||
bd.setDestroyMethodNames("customDestroyMethod", "destroyMethod");
|
||||
RuntimeHints runtimeHints = new RuntimeHints();
|
||||
getDefaultContribution(new DefaultListableBeanFactory(), bd).registerRuntimeHints(runtimeHints);
|
||||
assertThat(runtimeHints.reflection().getTypeHint(InitDestroyBean.class)).satisfies(hint ->
|
||||
assertThat(hint.methods()).anySatisfy(invokeMethodHint("customDestroyMethod"))
|
||||
.anySatisfy(invokeMethodHint("destroyMethod")).hasSize(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerRuntimeHintsWithNoPropertyValuesDoesNotAccessRuntimeHints() {
|
||||
RootBeanDefinition bd = new RootBeanDefinition(String.class);
|
||||
@@ -432,12 +479,12 @@ class BeanRegistrationBeanFactoryContributionTests {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(BaseFactoryBean.class));
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.methods()).singleElement()
|
||||
.satisfies(methodHint("setName", String.class));
|
||||
.satisfies(invokeMethodHint("setName", String.class));
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
}).anySatisfy(typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(IntegerFactoryBean.class));
|
||||
assertThat(typeHint.constructors()).singleElement()
|
||||
.satisfies(constructorHint(Environment.class));
|
||||
.satisfies(introspectConstructorHint(Environment.class));
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
}).hasSize(2);
|
||||
@@ -453,8 +500,8 @@ class BeanRegistrationBeanFactoryContributionTests {
|
||||
assertThat(reflectionHints.typeHints()).singleElement().satisfies(typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(NameAndCountersComponent.class));
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.methods()).anySatisfy(methodHint("setName", String.class))
|
||||
.anySatisfy(methodHint("setCounter", Integer.class)).hasSize(2);
|
||||
assertThat(typeHint.methods()).anySatisfy(invokeMethodHint("setName", String.class))
|
||||
.anySatisfy(invokeMethodHint("setCounter", Integer.class)).hasSize(2);
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
});
|
||||
}
|
||||
@@ -473,14 +520,14 @@ class BeanRegistrationBeanFactoryContributionTests {
|
||||
assertThat(reflectionHints.typeHints()).anySatisfy(typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(NameAndCountersComponent.class));
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(methodHint("setCounter", Integer.class));
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(invokeMethodHint("setCounter", Integer.class));
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
}).anySatisfy(typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(BaseFactoryBean.class));
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(methodHint("setName", String.class));
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(invokeMethodHint("setName", String.class));
|
||||
}).anySatisfy(typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(IntegerFactoryBean.class));
|
||||
assertThat(typeHint.constructors()).singleElement().satisfies(constructorHint(Environment.class));
|
||||
assertThat(typeHint.constructors()).singleElement().satisfies(introspectConstructorHint(Environment.class));
|
||||
}).hasSize(3);
|
||||
}
|
||||
|
||||
@@ -499,32 +546,37 @@ class BeanRegistrationBeanFactoryContributionTests {
|
||||
assertThat(reflectionHints.typeHints()).anySatisfy(typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(NameAndCountersComponent.class));
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(methodHint("setCounters", List.class));
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(invokeMethodHint("setCounters", List.class));
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
}).anySatisfy(typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(BaseFactoryBean.class));
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(methodHint("setName", String.class));
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(invokeMethodHint("setName", String.class));
|
||||
}).anySatisfy(typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(IntegerFactoryBean.class));
|
||||
assertThat(typeHint.constructors()).singleElement().satisfies(constructorHint(Environment.class));
|
||||
assertThat(typeHint.constructors()).singleElement().satisfies(introspectConstructorHint(Environment.class));
|
||||
}).anySatisfy(typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(AnotherIntegerFactoryBean.class));
|
||||
assertThat(typeHint.constructors()).singleElement().satisfies(constructorHint(Environment.class));
|
||||
assertThat(typeHint.constructors()).singleElement().satisfies(introspectConstructorHint(Environment.class));
|
||||
}).hasSize(4);
|
||||
}
|
||||
|
||||
private Consumer<ExecutableHint> methodHint(String name, Class<?>... parameterTypes) {
|
||||
private Consumer<ExecutableHint> invokeMethodHint(String name, Class<?>... parameterTypes) {
|
||||
return executableHint(ExecutableMode.INVOKE, name, parameterTypes);
|
||||
}
|
||||
|
||||
private Consumer<ExecutableHint> introspectConstructorHint(Class<?>... parameterTypes) {
|
||||
return executableHint(ExecutableMode.INTROSPECT, "<init>", parameterTypes);
|
||||
}
|
||||
|
||||
private Consumer<ExecutableHint> executableHint(ExecutableMode mode, String name, Class<?>... parameterTypes) {
|
||||
return executableHint -> {
|
||||
assertThat(executableHint.getName()).isEqualTo(name);
|
||||
assertThat(executableHint.getParameterTypes()).containsExactly(Arrays.stream(parameterTypes)
|
||||
.map(TypeReference::of).toArray(TypeReference[]::new));
|
||||
assertThat(executableHint.getModes()).containsExactly(mode);
|
||||
};
|
||||
}
|
||||
|
||||
private Consumer<ExecutableHint> constructorHint(Class<?>... parameterTypes) {
|
||||
return methodHint("<init>", parameterTypes);
|
||||
}
|
||||
|
||||
private Consumer<DefaultListableBeanFactory> hasBeanDefinition(Consumer<RootBeanDefinition> bd) {
|
||||
return beanFactory -> {
|
||||
assertThat(beanFactory.getBeanDefinitionNames()).contains("test");
|
||||
|
||||
Reference in New Issue
Block a user