17
17
package org .springframework .beans .factory .support ;
18
18
19
19
import java .lang .reflect .Method ;
20
+ import java .util .concurrent .ExecutorService ;
20
21
21
22
import org .junit .jupiter .api .Test ;
22
23
@@ -59,13 +60,38 @@ void setInstanceDoesNotOverrideResolvedFactoryMethodWithNull() {
59
60
}
60
61
61
62
@ Test
62
- void resolveDestroyMethodWithMatchingCandidateReplacedInferredVaue () {
63
+ void resolveDestroyMethodWithMatchingCandidateReplacedForCloseMethod () {
63
64
RootBeanDefinition beanDefinition = new RootBeanDefinition (BeanWithCloseMethod .class );
64
65
beanDefinition .setDestroyMethodName (AbstractBeanDefinition .INFER_METHOD );
65
66
beanDefinition .resolveDestroyMethodIfNecessary ();
66
67
assertThat (beanDefinition .getDestroyMethodNames ()).containsExactly ("close" );
67
68
}
68
69
70
+ @ Test
71
+ void resolveDestroyMethodWithMatchingCandidateReplacedForShutdownMethod () {
72
+ RootBeanDefinition beanDefinition = new RootBeanDefinition (BeanWithShutdownMethod .class );
73
+ beanDefinition .setDestroyMethodName (AbstractBeanDefinition .INFER_METHOD );
74
+ beanDefinition .resolveDestroyMethodIfNecessary ();
75
+ assertThat (beanDefinition .getDestroyMethodNames ()).containsExactly ("shutdown" );
76
+ }
77
+
78
+ @ Test
79
+ void resolveDestroyMethodWithMatchingCandidateReplacedForExecutorService () {
80
+ RootBeanDefinition beanDefinition = new RootBeanDefinition (BeanImplementingExecutorService .class );
81
+ beanDefinition .setDestroyMethodName (AbstractBeanDefinition .INFER_METHOD );
82
+ beanDefinition .resolveDestroyMethodIfNecessary ();
83
+ assertThat (beanDefinition .getDestroyMethodNames ()).containsExactly ("shutdown" );
84
+ // even on JDK 19+ where the ExecutorService interface declares a default AutoCloseable implementation
85
+ }
86
+
87
+ @ Test
88
+ void resolveDestroyMethodWithMatchingCandidateReplacedForAutoCloseableExecutorService () {
89
+ RootBeanDefinition beanDefinition = new RootBeanDefinition (BeanImplementingExecutorServiceAndAutoCloseable .class );
90
+ beanDefinition .setDestroyMethodName (AbstractBeanDefinition .INFER_METHOD );
91
+ beanDefinition .resolveDestroyMethodIfNecessary ();
92
+ assertThat (beanDefinition .getDestroyMethodNames ()).containsExactly ("close" );
93
+ }
94
+
69
95
@ Test
70
96
void resolveDestroyMethodWithNoCandidateSetDestroyMethodNameToNull () {
71
97
RootBeanDefinition beanDefinition = new RootBeanDefinition (BeanWithNoDestroyMethod .class );
@@ -90,6 +116,25 @@ public void close() {
90
116
}
91
117
92
118
119
+ static class BeanWithShutdownMethod {
120
+
121
+ public void shutdown () {
122
+ }
123
+ }
124
+
125
+
126
+ abstract static class BeanImplementingExecutorService implements ExecutorService {
127
+ }
128
+
129
+
130
+ abstract static class BeanImplementingExecutorServiceAndAutoCloseable implements ExecutorService , AutoCloseable {
131
+
132
+ @ Override
133
+ public void close () {
134
+ }
135
+ }
136
+
137
+
93
138
static class BeanWithNoDestroyMethod {
94
139
}
95
140
0 commit comments