Skip to content

Commit 4c823a3

Browse files
jhoellerunknown
authored andcommitted
DisposableBeanAdapter detects "shutdown" as a destroy method as well (for EHCache CacheManager setup)
Issue: SPR-9713
1 parent 0dcc0f2 commit 4c823a3

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,6 +61,8 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
6161

6262
private static final String CLOSE_METHOD_NAME = "close";
6363

64+
private static final String SHUTDOWN_METHOD_NAME = "shutdown";
65+
6466
private static final Log logger = LogFactory.getLog(DisposableBeanAdapter.class);
6567

6668
private static Class closeableInterface;
@@ -176,7 +178,12 @@ private String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition bea
176178
return bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();
177179
}
178180
catch (NoSuchMethodException ex) {
179-
// no candidate destroy method found
181+
try {
182+
return bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();
183+
}
184+
catch (NoSuchMethodException ex2) {
185+
// no candidate destroy method found
186+
}
180187
}
181188
}
182189
return null;

spring-context/src/test/java/org/springframework/context/annotation/DestroyMethodInferenceTests.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,21 @@
1616

1717
package org.springframework.context.annotation;
1818

19-
import static org.hamcrest.CoreMatchers.is;
20-
import static org.junit.Assert.assertThat;
21-
2219
import java.io.Closeable;
2320
import java.io.IOException;
2421

2522
import org.junit.Test;
23+
2624
import org.springframework.context.ConfigurableApplicationContext;
2725
import org.springframework.context.support.GenericXmlApplicationContext;
2826

27+
import static org.hamcrest.CoreMatchers.*;
28+
import static org.junit.Assert.*;
29+
30+
/**
31+
* @author Chris Beams
32+
* @author Juergen Hoeller
33+
*/
2934
public class DestroyMethodInferenceTests {
3035

3136
@Test
@@ -39,6 +44,7 @@ public void beanMethods() {
3944
WithInheritedCloseMethod c4 = ctx.getBean("c4", WithInheritedCloseMethod.class);
4045
WithInheritedCloseMethod c5 = ctx.getBean("c5", WithInheritedCloseMethod.class);
4146
WithNoCloseMethod c6 = ctx.getBean("c6", WithNoCloseMethod.class);
47+
WithLocalShutdownMethod c7 = ctx.getBean("c7", WithLocalShutdownMethod.class);
4248

4349
assertThat(c0.closed, is(false));
4450
assertThat(c1.closed, is(false));
@@ -47,6 +53,7 @@ public void beanMethods() {
4753
assertThat(c4.closed, is(false));
4854
assertThat(c5.closed, is(false));
4955
assertThat(c6.closed, is(false));
56+
assertThat(c7.closed, is(false));
5057
ctx.close();
5158
assertThat("c0", c0.closed, is(true));
5259
assertThat("c1", c1.closed, is(true));
@@ -55,6 +62,7 @@ public void beanMethods() {
5562
assertThat("c4", c4.closed, is(true));
5663
assertThat("c5", c5.closed, is(true));
5764
assertThat("c6", c6.closed, is(false));
65+
assertThat("c7", c7.closed, is(true));
5866
}
5967

6068
@Test
@@ -121,6 +129,11 @@ public void other() {
121129
public WithNoCloseMethod c6() {
122130
return new WithNoCloseMethod();
123131
}
132+
133+
@Bean
134+
public WithLocalShutdownMethod c7() {
135+
return new WithLocalShutdownMethod();
136+
}
124137
}
125138

126139

@@ -149,4 +162,12 @@ public void close() throws IOException {
149162
static class WithNoCloseMethod {
150163
boolean closed = false;
151164
}
165+
166+
static class WithLocalShutdownMethod {
167+
boolean closed = false;
168+
public void shutdown() {
169+
closed = true;
170+
}
171+
}
172+
152173
}

0 commit comments

Comments
 (0)