1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .context .annotation ;
18
18
19
19
import java .io .Closeable ;
20
- import java .io .IOException ;
21
20
22
21
import org .junit .Test ;
23
22
23
+ import org .springframework .beans .factory .DisposableBean ;
24
24
import org .springframework .context .ConfigurableApplicationContext ;
25
25
import org .springframework .context .support .GenericXmlApplicationContext ;
26
26
@@ -36,8 +36,7 @@ public class DestroyMethodInferenceTests {
36
36
37
37
@ Test
38
38
public void beanMethods () {
39
- ConfigurableApplicationContext ctx =
40
- new AnnotationConfigApplicationContext (Config .class );
39
+ ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (Config .class );
41
40
WithExplicitDestroyMethod c0 = ctx .getBean (WithExplicitDestroyMethod .class );
42
41
WithLocalCloseMethod c1 = ctx .getBean ("c1" , WithLocalCloseMethod .class );
43
42
WithLocalCloseMethod c2 = ctx .getBean ("c2" , WithLocalCloseMethod .class );
@@ -47,6 +46,7 @@ public void beanMethods() {
47
46
WithNoCloseMethod c6 = ctx .getBean ("c6" , WithNoCloseMethod .class );
48
47
WithLocalShutdownMethod c7 = ctx .getBean ("c7" , WithLocalShutdownMethod .class );
49
48
WithInheritedCloseMethod c8 = ctx .getBean ("c8" , WithInheritedCloseMethod .class );
49
+ WithDisposableBean c9 = ctx .getBean ("c9" , WithDisposableBean .class );
50
50
51
51
assertThat (c0 .closed , is (false ));
52
52
assertThat (c1 .closed , is (false ));
@@ -57,6 +57,7 @@ public void beanMethods() {
57
57
assertThat (c6 .closed , is (false ));
58
58
assertThat (c7 .closed , is (false ));
59
59
assertThat (c8 .closed , is (false ));
60
+ assertThat (c9 .closed , is (false ));
60
61
ctx .close ();
61
62
assertThat ("c0" , c0 .closed , is (true ));
62
63
assertThat ("c1" , c1 .closed , is (true ));
@@ -67,6 +68,7 @@ public void beanMethods() {
67
68
assertThat ("c6" , c6 .closed , is (false ));
68
69
assertThat ("c7" , c7 .closed , is (true ));
69
70
assertThat ("c8" , c8 .closed , is (false ));
71
+ assertThat ("c9" , c9 .closed , is (true ));
70
72
}
71
73
72
74
@ Test
@@ -91,9 +93,11 @@ public void xml() {
91
93
assertThat (x8 .closed , is (false ));
92
94
}
93
95
96
+
94
97
@ Configuration
95
98
static class Config {
96
- @ Bean (destroyMethod ="explicitClose" )
99
+
100
+ @ Bean (destroyMethod = "explicitClose" )
97
101
public WithExplicitDestroyMethod c0 () {
98
102
return new WithExplicitDestroyMethod ();
99
103
}
@@ -118,12 +122,12 @@ public Closeable c4() {
118
122
return new WithInheritedCloseMethod ();
119
123
}
120
124
121
- @ Bean (destroyMethod = "other" )
125
+ @ Bean (destroyMethod = "other" )
122
126
public WithInheritedCloseMethod c5 () {
123
127
return new WithInheritedCloseMethod () {
124
128
@ Override
125
- public void close () throws IOException {
126
- throw new RuntimeException ("close() should not be called" );
129
+ public void close () {
130
+ throw new IllegalStateException ("close() should not be called" );
127
131
}
128
132
@ SuppressWarnings ("unused" )
129
133
public void other () {
@@ -146,37 +150,66 @@ public WithLocalShutdownMethod c7() {
146
150
public WithInheritedCloseMethod c8 () {
147
151
return new WithInheritedCloseMethod ();
148
152
}
153
+
154
+ @ Bean (destroyMethod = "" )
155
+ public WithDisposableBean c9 () {
156
+ return new WithDisposableBean ();
157
+ }
149
158
}
150
159
151
160
152
161
static class WithExplicitDestroyMethod {
162
+
153
163
boolean closed = false ;
164
+
154
165
public void explicitClose () {
155
166
closed = true ;
156
167
}
157
168
}
158
169
170
+
159
171
static class WithLocalCloseMethod {
172
+
160
173
boolean closed = false ;
174
+
161
175
public void close () {
162
176
closed = true ;
163
177
}
164
178
}
165
179
180
+
166
181
static class WithInheritedCloseMethod implements Closeable {
182
+
183
+ boolean closed = false ;
184
+
185
+ @ Override
186
+ public void close () {
187
+ closed = true ;
188
+ }
189
+ }
190
+
191
+
192
+ static class WithDisposableBean implements DisposableBean {
193
+
167
194
boolean closed = false ;
195
+
168
196
@ Override
169
- public void close () throws IOException {
197
+ public void destroy () {
170
198
closed = true ;
171
199
}
172
200
}
173
201
202
+
174
203
static class WithNoCloseMethod {
204
+
175
205
boolean closed = false ;
176
206
}
177
207
208
+
178
209
static class WithLocalShutdownMethod {
210
+
179
211
boolean closed = false ;
212
+
180
213
public void shutdown () {
181
214
closed = true ;
182
215
}
0 commit comments