@@ -2511,10 +2511,74 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
25112511 return bean ;
25122512 }
25132513 });
2514- BeanWithDestroyMethod .closed = false ;
2514+ BeanWithDestroyMethod .closeCount = 0 ;
25152515 lbf .preInstantiateSingletons ();
25162516 lbf .destroySingletons ();
2517- assertTrue ("Destroy method invoked" , BeanWithDestroyMethod .closed );
2517+ assertEquals ("Destroy methods invoked" , 1 , BeanWithDestroyMethod .closeCount );
2518+ }
2519+
2520+ @ Test
2521+ public void testDestroyMethodOnInnerBean () {
2522+ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory ();
2523+ RootBeanDefinition innerBd = new RootBeanDefinition (BeanWithDestroyMethod .class );
2524+ innerBd .setDestroyMethodName ("close" );
2525+ RootBeanDefinition bd = new RootBeanDefinition (BeanWithDestroyMethod .class );
2526+ bd .setDestroyMethodName ("close" );
2527+ bd .getPropertyValues ().add ("inner" , innerBd );
2528+ lbf .registerBeanDefinition ("test" , bd );
2529+ BeanWithDestroyMethod .closeCount = 0 ;
2530+ lbf .preInstantiateSingletons ();
2531+ lbf .destroySingletons ();
2532+ assertEquals ("Destroy methods invoked" , 2 , BeanWithDestroyMethod .closeCount );
2533+ }
2534+
2535+ @ Test
2536+ public void testDestroyMethodOnInnerBeanAsPrototype () {
2537+ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory ();
2538+ RootBeanDefinition innerBd = new RootBeanDefinition (BeanWithDestroyMethod .class );
2539+ innerBd .setScope (RootBeanDefinition .SCOPE_PROTOTYPE );
2540+ innerBd .setDestroyMethodName ("close" );
2541+ RootBeanDefinition bd = new RootBeanDefinition (BeanWithDestroyMethod .class );
2542+ bd .setDestroyMethodName ("close" );
2543+ bd .getPropertyValues ().add ("inner" , innerBd );
2544+ lbf .registerBeanDefinition ("test" , bd );
2545+ BeanWithDestroyMethod .closeCount = 0 ;
2546+ lbf .preInstantiateSingletons ();
2547+ lbf .destroySingletons ();
2548+ assertEquals ("Destroy methods invoked" , 1 , BeanWithDestroyMethod .closeCount );
2549+ }
2550+
2551+ @ Test
2552+ public void testDestroyMethodOnInnerBeanAsCustomScope () {
2553+ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory ();
2554+ RootBeanDefinition innerBd = new RootBeanDefinition (BeanWithDestroyMethod .class );
2555+ innerBd .setScope ("custom" );
2556+ innerBd .setDestroyMethodName ("close" );
2557+ RootBeanDefinition bd = new RootBeanDefinition (BeanWithDestroyMethod .class );
2558+ bd .setDestroyMethodName ("close" );
2559+ bd .getPropertyValues ().add ("inner" , innerBd );
2560+ lbf .registerBeanDefinition ("test" , bd );
2561+ BeanWithDestroyMethod .closeCount = 0 ;
2562+ lbf .preInstantiateSingletons ();
2563+ lbf .destroySingletons ();
2564+ assertEquals ("Destroy methods not invoked" , 1 , BeanWithDestroyMethod .closeCount );
2565+ }
2566+
2567+ @ Test
2568+ public void testDestroyMethodOnInnerBeanAsCustomScopeWithinPrototype () {
2569+ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory ();
2570+ RootBeanDefinition innerBd = new RootBeanDefinition (BeanWithDestroyMethod .class );
2571+ innerBd .setScope ("custom" );
2572+ innerBd .setDestroyMethodName ("close" );
2573+ RootBeanDefinition bd = new RootBeanDefinition (BeanWithDestroyMethod .class );
2574+ bd .setScope (RootBeanDefinition .SCOPE_PROTOTYPE );
2575+ bd .setDestroyMethodName ("close" );
2576+ bd .getPropertyValues ().add ("inner" , innerBd );
2577+ lbf .registerBeanDefinition ("test" , bd );
2578+ BeanWithDestroyMethod .closeCount = 0 ;
2579+ Object prototypeInstance = lbf .getBean ("test" );
2580+ lbf .destroyBean ("test" , prototypeInstance );
2581+ assertEquals ("Destroy methods not invoked" , 1 , BeanWithDestroyMethod .closeCount );
25182582 }
25192583
25202584 @ Test
@@ -2780,7 +2844,7 @@ public void testByTypeLookupIsFastEnough() {
27802844
27812845 @ Test (timeout = 1000 )
27822846 public void testRegistrationOfManyBeanDefinitionsIsFastEnough () {
2783- // Assume.group(TestGroup.PERFORMANCE);
2847+ Assume .group (TestGroup .PERFORMANCE );
27842848 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
27852849 bf .registerBeanDefinition ("b" , new RootBeanDefinition (B .class ));
27862850 // bf.getBean("b");
@@ -2792,7 +2856,7 @@ public void testRegistrationOfManyBeanDefinitionsIsFastEnough() {
27922856
27932857 @ Test (timeout = 1000 )
27942858 public void testRegistrationOfManySingletonsIsFastEnough () {
2795- // Assume.group(TestGroup.PERFORMANCE);
2859+ Assume .group (TestGroup .PERFORMANCE );
27962860 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
27972861 bf .registerBeanDefinition ("b" , new RootBeanDefinition (B .class ));
27982862 // bf.getBean("b");
@@ -2913,10 +2977,16 @@ public void close() {
29132977
29142978 public static class BeanWithDestroyMethod {
29152979
2916- private static boolean closed ;
2980+ private static int closeCount = 0 ;
2981+
2982+ private BeanWithDestroyMethod inner ;
2983+
2984+ public void setInner (BeanWithDestroyMethod inner ) {
2985+ this .inner = inner ;
2986+ }
29172987
29182988 public void close () {
2919- closed = true ;
2989+ closeCount ++ ;
29202990 }
29212991 }
29222992
0 commit comments