Revert "Make sure inferred destroy method is set on the original bean definition"
This reverts commit 2e1538a20b.
This commit is contained in:
parent
cd2b7afc87
commit
0a9db7cc47
|
|
@ -41,9 +41,7 @@ import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
|
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
|
||||||
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
|
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
|
||||||
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
|
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
|
||||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
|
||||||
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
|
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
|
||||||
import org.springframework.beans.factory.support.RegisteredBean;
|
import org.springframework.beans.factory.support.RegisteredBean;
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
|
|
@ -160,11 +158,9 @@ public class InitDestroyAnnotationBeanPostProcessor implements DestructionAwareB
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
|
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
|
||||||
AbstractBeanDefinition beanDefinition = getOriginalBeanDefinition(registeredBean);
|
RootBeanDefinition beanDefinition = registeredBean.getMergedBeanDefinition();
|
||||||
if (beanDefinition != null) {
|
|
||||||
RootBeanDefinition mergedBeanDefinition = registeredBean.getMergedBeanDefinition();
|
|
||||||
beanDefinition.resolveDestroyMethodIfNecessary();
|
beanDefinition.resolveDestroyMethodIfNecessary();
|
||||||
LifecycleMetadata metadata = findInjectionMetadata(mergedBeanDefinition, registeredBean.getBeanClass());
|
LifecycleMetadata metadata = findInjectionMetadata(beanDefinition, registeredBean.getBeanClass());
|
||||||
if (!CollectionUtils.isEmpty(metadata.initMethods)) {
|
if (!CollectionUtils.isEmpty(metadata.initMethods)) {
|
||||||
String[] initMethodNames = safeMerge(beanDefinition.getInitMethodNames(), metadata.initMethods);
|
String[] initMethodNames = safeMerge(beanDefinition.getInitMethodNames(), metadata.initMethods);
|
||||||
beanDefinition.setInitMethodNames(initMethodNames);
|
beanDefinition.setInitMethodNames(initMethodNames);
|
||||||
|
|
@ -173,17 +169,9 @@ public class InitDestroyAnnotationBeanPostProcessor implements DestructionAwareB
|
||||||
String[] destroyMethodNames = safeMerge(beanDefinition.getDestroyMethodNames(), metadata.destroyMethods);
|
String[] destroyMethodNames = safeMerge(beanDefinition.getDestroyMethodNames(), metadata.destroyMethods);
|
||||||
beanDefinition.setDestroyMethodNames(destroyMethodNames);
|
beanDefinition.setDestroyMethodNames(destroyMethodNames);
|
||||||
}
|
}
|
||||||
registeredBean.getBeanFactory().clearMetadataCache();
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private AbstractBeanDefinition getOriginalBeanDefinition(RegisteredBean registeredBean) {
|
|
||||||
BeanDefinition beanDefinition = registeredBean.getBeanFactory().getBeanDefinition(registeredBean.getBeanName());
|
|
||||||
return (beanDefinition instanceof AbstractBeanDefinition abd ? abd : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private LifecycleMetadata findInjectionMetadata(RootBeanDefinition beanDefinition, Class<?> beanType) {
|
private LifecycleMetadata findInjectionMetadata(RootBeanDefinition beanDefinition, Class<?> beanType) {
|
||||||
LifecycleMetadata metadata = findLifecycleMetadata(beanType);
|
LifecycleMetadata metadata = findLifecycleMetadata(beanType);
|
||||||
metadata.checkConfigMembers(beanDefinition);
|
metadata.checkConfigMembers(beanDefinition);
|
||||||
|
|
|
||||||
|
|
@ -1186,14 +1186,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve the inferred destroy method if necessary.
|
|
||||||
* @since 6.0
|
|
||||||
*/
|
|
||||||
public void resolveDestroyMethodIfNecessary() {
|
|
||||||
setDestroyMethodNames(DisposableBeanAdapter
|
|
||||||
.inferDestroyMethodsIfNecessary(getResolvableType().toClass(), this));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public declaration of Object's {@code clone()} method.
|
* Public declaration of Object's {@code clone()} method.
|
||||||
|
|
|
||||||
|
|
@ -344,14 +344,13 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
|
||||||
* interfaces, reflectively calling the "close" method on implementing beans as well.
|
* interfaces, reflectively calling the "close" method on implementing beans as well.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
static String[] inferDestroyMethodsIfNecessary(Class<?> target, AbstractBeanDefinition beanDefinition) {
|
static String[] inferDestroyMethodsIfNecessary(Class<?> target, RootBeanDefinition beanDefinition) {
|
||||||
String[] destroyMethodNames = beanDefinition.getDestroyMethodNames();
|
String[] destroyMethodNames = beanDefinition.getDestroyMethodNames();
|
||||||
if (destroyMethodNames != null && destroyMethodNames.length > 1) {
|
if (destroyMethodNames != null && destroyMethodNames.length > 1) {
|
||||||
return destroyMethodNames;
|
return destroyMethodNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
String destroyMethodName = (beanDefinition instanceof RootBeanDefinition rbd
|
String destroyMethodName = beanDefinition.resolvedDestroyMethodName;
|
||||||
? rbd.resolvedDestroyMethodName : null);
|
|
||||||
if (destroyMethodName == null) {
|
if (destroyMethodName == null) {
|
||||||
destroyMethodName = beanDefinition.getDestroyMethodName();
|
destroyMethodName = beanDefinition.getDestroyMethodName();
|
||||||
boolean autoCloseable = (AutoCloseable.class.isAssignableFrom(target));
|
boolean autoCloseable = (AutoCloseable.class.isAssignableFrom(target));
|
||||||
|
|
@ -379,9 +378,7 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (beanDefinition instanceof RootBeanDefinition rbd) {
|
beanDefinition.resolvedDestroyMethodName = (destroyMethodName != null ? destroyMethodName : "");
|
||||||
rbd.resolvedDestroyMethodName = (destroyMethodName != null ? destroyMethodName : "");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return (StringUtils.hasLength(destroyMethodName) ? new String[] {destroyMethodName} : null);
|
return (StringUtils.hasLength(destroyMethodName) ? new String[] {destroyMethodName} : null);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -550,6 +550,15 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve the inferred destroy method if necessary.
|
||||||
|
* @since 6.0
|
||||||
|
*/
|
||||||
|
public void resolveDestroyMethodIfNecessary() {
|
||||||
|
setDestroyMethodNames(DisposableBeanAdapter
|
||||||
|
.inferDestroyMethodsIfNecessary(getResolvableType().toClass(), this));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register an externally managed configuration destruction method —
|
* Register an externally managed configuration destruction method —
|
||||||
* for example, a method annotated with JSR-250's
|
* for example, a method annotated with JSR-250's
|
||||||
|
|
|
||||||
|
|
@ -90,19 +90,6 @@ class InitDestroyAnnotationBeanPostProcessorTests {
|
||||||
assertThat(mergedBeanDefinition.getDestroyMethodNames()).containsExactly("close");
|
assertThat(mergedBeanDefinition.getDestroyMethodNames()).containsExactly("close");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void processAheadOfTimeWhenHasInferredDestroyMethodIsRetainedIfMergedBeanDefinitionIsStale() {
|
|
||||||
RootBeanDefinition beanDefinition = new RootBeanDefinition(InferredDestroyBean.class);
|
|
||||||
beanDefinition.setDestroyMethodNames(AbstractBeanDefinition.INFER_METHOD);
|
|
||||||
processAheadOfTime(beanDefinition);
|
|
||||||
RootBeanDefinition mergedBeanDefinition = getMergedBeanDefinition();
|
|
||||||
assertThat(mergedBeanDefinition.getInitMethodNames()).isNull();
|
|
||||||
assertThat(mergedBeanDefinition.getDestroyMethodNames()).containsExactly("close");
|
|
||||||
RootBeanDefinition originalBeanDefinition = (RootBeanDefinition) this.beanFactory.getBeanDefinition("test");
|
|
||||||
assertThat(originalBeanDefinition.getInitMethodNames()).isNull();
|
|
||||||
assertThat(originalBeanDefinition.getDestroyMethodNames()).containsExactly("close");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void processAheadOfTimeWhenHasInferredDestroyMethodAndNoCandidateDoesNotMutateRootBeanDefinition() {
|
void processAheadOfTimeWhenHasInferredDestroyMethodAndNoCandidateDoesNotMutateRootBeanDefinition() {
|
||||||
RootBeanDefinition beanDefinition = new RootBeanDefinition(NoInitDestroyBean.class);
|
RootBeanDefinition beanDefinition = new RootBeanDefinition(NoInitDestroyBean.class);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue