Browse Source

DisposableBeanAdapter detects "shutdown" as a destroy method as well (for EHCache CacheManager setup)

Issue: SPR-9713
pull/201/head
Juergen Hoeller 12 years ago committed by unknown
parent
commit
4c823a3f9b
  1. 11
      spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java
  2. 29
      spring-context/src/test/java/org/springframework/context/annotation/DestroyMethodInferenceTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -61,6 +61,8 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable { @@ -61,6 +61,8 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
private static final String CLOSE_METHOD_NAME = "close";
private static final String SHUTDOWN_METHOD_NAME = "shutdown";
private static final Log logger = LogFactory.getLog(DisposableBeanAdapter.class);
private static Class closeableInterface;
@ -176,7 +178,12 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable { @@ -176,7 +178,12 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
return bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();
}
catch (NoSuchMethodException ex) {
// no candidate destroy method found
try {
return bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();
}
catch (NoSuchMethodException ex2) {
// no candidate destroy method found
}
}
}
return null;

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,16 +16,21 @@ @@ -16,16 +16,21 @@
package org.springframework.context.annotation;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.io.Closeable;
import java.io.IOException;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
* @author Chris Beams
* @author Juergen Hoeller
*/
public class DestroyMethodInferenceTests {
@Test
@ -39,6 +44,7 @@ public class DestroyMethodInferenceTests { @@ -39,6 +44,7 @@ public class DestroyMethodInferenceTests {
WithInheritedCloseMethod c4 = ctx.getBean("c4", WithInheritedCloseMethod.class);
WithInheritedCloseMethod c5 = ctx.getBean("c5", WithInheritedCloseMethod.class);
WithNoCloseMethod c6 = ctx.getBean("c6", WithNoCloseMethod.class);
WithLocalShutdownMethod c7 = ctx.getBean("c7", WithLocalShutdownMethod.class);
assertThat(c0.closed, is(false));
assertThat(c1.closed, is(false));
@ -47,6 +53,7 @@ public class DestroyMethodInferenceTests { @@ -47,6 +53,7 @@ public class DestroyMethodInferenceTests {
assertThat(c4.closed, is(false));
assertThat(c5.closed, is(false));
assertThat(c6.closed, is(false));
assertThat(c7.closed, is(false));
ctx.close();
assertThat("c0", c0.closed, is(true));
assertThat("c1", c1.closed, is(true));
@ -55,6 +62,7 @@ public class DestroyMethodInferenceTests { @@ -55,6 +62,7 @@ public class DestroyMethodInferenceTests {
assertThat("c4", c4.closed, is(true));
assertThat("c5", c5.closed, is(true));
assertThat("c6", c6.closed, is(false));
assertThat("c7", c7.closed, is(true));
}
@Test
@ -121,6 +129,11 @@ public class DestroyMethodInferenceTests { @@ -121,6 +129,11 @@ public class DestroyMethodInferenceTests {
public WithNoCloseMethod c6() {
return new WithNoCloseMethod();
}
@Bean
public WithLocalShutdownMethod c7() {
return new WithLocalShutdownMethod();
}
}
@ -149,4 +162,12 @@ public class DestroyMethodInferenceTests { @@ -149,4 +162,12 @@ public class DestroyMethodInferenceTests {
static class WithNoCloseMethod {
boolean closed = false;
}
static class WithLocalShutdownMethod {
boolean closed = false;
public void shutdown() {
closed = true;
}
}
}

Loading…
Cancel
Save