Skip to content

Commit

Permalink
Merge branch '5.3.x'
Browse files Browse the repository at this point in the history
# Conflicts:
#	spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java
#	spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
#	spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java
#	spring-context/src/test/java/org/springframework/context/annotation/Spr3775InitDestroyLifecycleTests.java
  • Loading branch information
sbrannen committed Mar 1, 2022
2 parents 27ee9cd + d67034f commit 466dd82
Show file tree
Hide file tree
Showing 5 changed files with 382 additions and 15 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* 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.
Expand Down Expand Up @@ -302,7 +302,7 @@ public void checkConfigMembers(RootBeanDefinition beanDefinition) {
beanDefinition.registerExternallyManagedInitMethod(methodIdentifier);
checkedInitMethods.add(element);
if (logger.isTraceEnabled()) {
logger.trace("Registered init method on class [" + this.targetClass.getName() + "]: " + element);
logger.trace("Registered init method on class [" + this.targetClass.getName() + "]: " + methodIdentifier);
}
}
}
Expand All @@ -313,7 +313,7 @@ public void checkConfigMembers(RootBeanDefinition beanDefinition) {
beanDefinition.registerExternallyManagedDestroyMethod(methodIdentifier);
checkedDestroyMethods.add(element);
if (logger.isTraceEnabled()) {
logger.trace("Registered destroy method on class [" + this.targetClass.getName() + "]: " + element);
logger.trace("Registered destroy method on class [" + this.targetClass.getName() + "]: " + methodIdentifier);
}
}
}
Expand Down
Expand Up @@ -1780,7 +1780,7 @@ protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBea
throws Throwable {

boolean isInitializingBean = (bean instanceof InitializingBean);
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (isInitializingBean && (mbd == null || !mbd.hasAnyExternallyManagedInitMethod("afterPropertiesSet"))) {
if (logger.isTraceEnabled()) {
logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
}
Expand All @@ -1793,7 +1793,7 @@ protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBea
for (String initMethodName : initMethodNames) {
if (StringUtils.hasLength(initMethodName) &&
!(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
!mbd.hasAnyExternallyManagedInitMethod(initMethodName)) {
invokeCustomInitMethod(beanName, bean, mbd, initMethodName);
}
}
Expand Down
Expand Up @@ -48,6 +48,7 @@
* @author Juergen Hoeller
* @author Costin Leau
* @author Stephane Nicoll
* @author Sam Brannen
* @since 2.0
* @see AbstractBeanFactory
* @see org.springframework.beans.factory.DisposableBean
Expand Down Expand Up @@ -102,12 +103,12 @@ public DisposableBeanAdapter(Object bean, String beanName, RootBeanDefinition be
this.beanName = beanName;
this.nonPublicAccessAllowed = beanDefinition.isNonPublicAccessAllowed();
this.invokeDisposableBean = (bean instanceof DisposableBean &&
!beanDefinition.isExternallyManagedDestroyMethod(DESTROY_METHOD_NAME));
!beanDefinition.hasAnyExternallyManagedDestroyMethod(DESTROY_METHOD_NAME));

String[] destroyMethodNames = inferDestroyMethodsIfNecessary(bean, beanDefinition);
if (!ObjectUtils.isEmpty(destroyMethodNames) &&
!(this.invokeDisposableBean && DESTROY_METHOD_NAME.equals(destroyMethodNames[0])) &&
!beanDefinition.isExternallyManagedDestroyMethod(destroyMethodNames[0])) {
!beanDefinition.hasAnyExternallyManagedDestroyMethod(destroyMethodNames[0])) {

this.invokeAutoCloseable =
(bean instanceof AutoCloseable && CLOSE_METHOD_NAME.equals(destroyMethodNames[0]));
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* 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.
Expand Down Expand Up @@ -49,6 +49,7 @@
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Sam Brannen
* @see GenericBeanDefinition
* @see ChildBeanDefinition
*/
Expand Down Expand Up @@ -436,7 +437,7 @@ public void registerExternallyManagedConfigMember(Member configMember) {
}

/**
* Check whether the given method or field is an externally managed configuration member.
* Determine if the given method or field is an externally managed configuration member.
*/
public boolean isExternallyManagedConfigMember(Member configMember) {
synchronized (this.postProcessingLock) {
Expand All @@ -446,7 +447,7 @@ public boolean isExternallyManagedConfigMember(Member configMember) {
}

/**
* Return all externally managed configuration methods and fields (as an immutable Set).
* Get all externally managed configuration methods and fields (as an immutable Set).
* @since 5.3.11
*/
public Set<Member> getExternallyManagedConfigMembers() {
Expand All @@ -458,7 +459,15 @@ public Set<Member> getExternallyManagedConfigMembers() {
}

/**
* Register an externally managed configuration initialization method.
* Register an externally managed configuration initialization method &mdash;
* for example, a method annotated with JSR-250's
* {@link javax.annotation.PostConstruct} annotation.
* <p>The supplied {@code initMethod} may be the
* {@linkplain Method#getName() simple method name} for non-private methods or the
* {@linkplain org.springframework.util.ClassUtils#getQualifiedMethodName(Method)
* qualified method name} for {@code private} methods. A qualified name is
* necessary for {@code private} methods in order to disambiguate between
* multiple private methods with the same name within a class hierarchy.
*/
public void registerExternallyManagedInitMethod(String initMethod) {
synchronized (this.postProcessingLock) {
Expand All @@ -470,7 +479,10 @@ public void registerExternallyManagedInitMethod(String initMethod) {
}

/**
* Check whether the given method name indicates an externally managed initialization method.
* Determine if the given method name indicates an externally managed
* initialization method.
* <p>See {@link #registerExternallyManagedInitMethod} for details
* regarding the format for the supplied {@code initMethod}.
*/
public boolean isExternallyManagedInitMethod(String initMethod) {
synchronized (this.postProcessingLock) {
Expand All @@ -479,8 +491,40 @@ public boolean isExternallyManagedInitMethod(String initMethod) {
}
}

/**
* Determine if the given method name indicates an externally managed
* initialization method, regardless of method visibility.
* <p>In contrast to {@link #isExternallyManagedInitMethod(String)}, this
* method also returns {@code true} if there is a {@code private} externally
* managed initialization method that has been
* {@linkplain #registerExternallyManagedInitMethod(String) registered}
* using a qualified method name instead of a simple method name.
* @since 5.3.17
*/
boolean hasAnyExternallyManagedInitMethod(String initMethod) {
synchronized (this.postProcessingLock) {
if (isExternallyManagedInitMethod(initMethod)) {
return true;
}
if (this.externallyManagedInitMethods != null) {
for (String candidate : this.externallyManagedInitMethods) {
int indexOfDot = candidate.lastIndexOf(".");
if (indexOfDot >= 0) {
String methodName = candidate.substring(indexOfDot + 1);
if (methodName.equals(initMethod)) {
return true;
}
}
}
}
return false;
}
}

/**
* Return all externally managed initialization methods (as an immutable Set).
* <p>See {@link #registerExternallyManagedInitMethod} for details
* regarding the format for the initialization methods in the returned set.
* @since 5.3.11
*/
public Set<String> getExternallyManagedInitMethods() {
Expand All @@ -492,7 +536,15 @@ public Set<String> getExternallyManagedInitMethods() {
}

/**
* Register an externally managed configuration destruction method.
* Register an externally managed configuration destruction method &mdash;
* for example, a method annotated with JSR-250's
* {@link javax.annotation.PreDestroy} annotation.
* <p>The supplied {@code destroyMethod} may be the
* {@linkplain Method#getName() simple method name} for non-private methods or the
* {@linkplain org.springframework.util.ClassUtils#getQualifiedMethodName(Method)
* qualified method name} for {@code private} methods. A qualified name is
* necessary for {@code private} methods in order to disambiguate between
* multiple private methods with the same name within a class hierarchy.
*/
public void registerExternallyManagedDestroyMethod(String destroyMethod) {
synchronized (this.postProcessingLock) {
Expand All @@ -504,7 +556,10 @@ public void registerExternallyManagedDestroyMethod(String destroyMethod) {
}

/**
* Check whether the given method name indicates an externally managed destruction method.
* Determine if the given method name indicates an externally managed
* destruction method.
* <p>See {@link #registerExternallyManagedDestroyMethod} for details
* regarding the format for the supplied {@code destroyMethod}.
*/
public boolean isExternallyManagedDestroyMethod(String destroyMethod) {
synchronized (this.postProcessingLock) {
Expand All @@ -514,7 +569,39 @@ public boolean isExternallyManagedDestroyMethod(String destroyMethod) {
}

/**
* Return all externally managed destruction methods (as an immutable Set).
* Determine if the given method name indicates an externally managed
* destruction method, regardless of method visibility.
* <p>In contrast to {@link #isExternallyManagedDestroyMethod(String)}, this
* method also returns {@code true} if there is a {@code private} externally
* managed destruction method that has been
* {@linkplain #registerExternallyManagedDestroyMethod(String) registered}
* using a qualified method name instead of a simple method name.
* @since 5.3.17
*/
boolean hasAnyExternallyManagedDestroyMethod(String destroyMethod) {
synchronized (this.postProcessingLock) {
if (isExternallyManagedDestroyMethod(destroyMethod)) {
return true;
}
if (this.externallyManagedDestroyMethods != null) {
for (String candidate : this.externallyManagedDestroyMethods) {
int indexOfDot = candidate.lastIndexOf(".");
if (indexOfDot >= 0) {
String methodName = candidate.substring(indexOfDot + 1);
if (methodName.equals(destroyMethod)) {
return true;
}
}
}
}
return false;
}
}

/**
* Get all externally managed destruction methods (as an immutable Set).
* <p>See {@link #registerExternallyManagedDestroyMethod} for details
* regarding the format for the destruction methods in the returned set.
* @since 5.3.11
*/
public Set<String> getExternallyManagedDestroyMethods() {
Expand Down

0 comments on commit 466dd82

Please sign in to comment.