Skip to content

Commit c1f1cac

Browse files
committed
Remove DisposableBeanMethodInterceptor
Previously, any @configuration class was enhanced to namely implement DisposableBean in order to remove static callbacks that were registered for that class. This leads to problem if an ApplicationContext is created and destroyed within the lifecycle on another ApplicationContext in the same class loader. It turns out that the destruction callback is no longer necessary as the interceptors are now stateless: the VM is free to reclaim any of those if necessary. Issue: SPR-12445
1 parent 82651a0 commit c1f1cac

File tree

2 files changed

+4
-113
lines changed

2 files changed

+4
-113
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

+4-35
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.asm.Type;
2828
import org.springframework.beans.factory.BeanFactory;
2929
import org.springframework.beans.factory.BeanFactoryAware;
30-
import org.springframework.beans.factory.DisposableBean;
3130
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
3231
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3332
import org.springframework.beans.factory.support.SimpleInstantiationStrategy;
@@ -65,9 +64,9 @@
6564
*/
6665
class ConfigurationClassEnhancer {
6766

67+
// The callbacks to use. Note that these callbacks must be stateless.
6868
private static final Callback[] CALLBACKS = new Callback[] {
6969
new BeanMethodInterceptor(),
70-
new DisposableBeanMethodInterceptor(),
7170
new BeanFactoryAwareMethodInterceptor(),
7271
NoOp.INSTANCE
7372
};
@@ -139,15 +138,13 @@ private Class<?> createClass(Enhancer enhancer) {
139138
* Facilitates idempotent behavior for {@link ConfigurationClassEnhancer#enhance(Class)}
140139
* through checking to see if candidate classes are already assignable to it, e.g.
141140
* have already been enhanced.
142-
* <p>Also extends {@link DisposableBean} and {@link BeanFactoryAware}, as all
143-
* enhanced {@code @Configuration} classes require access to the {@link BeanFactory}
144-
* that created them and must de-register static CGLIB callbacks on destruction,
145-
* which is handled by the (private) {@code DisposableBeanMethodInterceptor}.
141+
* <p>Also extends {@link BeanFactoryAware}, as all enhanced {@code @Configuration}
142+
* classes require access to the {@link BeanFactory} that created them.
146143
* <p>Note that this interface is intended for framework-internal use only, however
147144
* must remain public in order to allow access to subclasses generated from other
148145
* packages (i.e. user code).
149146
*/
150-
public interface EnhancedConfiguration extends DisposableBean, BeanFactoryAware {
147+
public interface EnhancedConfiguration extends BeanFactoryAware {
151148
}
152149

153150

@@ -403,32 +400,4 @@ public boolean isMatch(Method candidateMethod) {
403400
}
404401
}
405402

406-
407-
/**
408-
* Intercepts the invocation of any {@link DisposableBean#destroy()} on @Configuration
409-
* class instances for the purpose of de-registering CGLIB callbacks. This helps avoid
410-
* garbage collection issues. See SPR-7901.
411-
* @see EnhancedConfiguration
412-
*/
413-
private static class DisposableBeanMethodInterceptor implements MethodInterceptor, ConditionalCallback {
414-
415-
@Override
416-
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
417-
Enhancer.registerStaticCallbacks(obj.getClass(), null);
418-
// Does the actual (non-CGLIB) superclass actually implement DisposableBean?
419-
// If so, call its dispose() method. If not, just exit.
420-
if (DisposableBean.class.isAssignableFrom(obj.getClass().getSuperclass())) {
421-
return proxy.invokeSuper(obj, args);
422-
}
423-
return null;
424-
}
425-
426-
@Override
427-
public boolean isMatch(Method candidateMethod) {
428-
return candidateMethod.getName().equals("destroy") &&
429-
candidateMethod.getParameterTypes().length == 0 &&
430-
DisposableBean.class.isAssignableFrom(candidateMethod.getDeclaringClass());
431-
}
432-
}
433-
434403
}

spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassCglibCallbackDeregistrationTests.java

-78
This file was deleted.

0 commit comments

Comments
 (0)