AbstractApplicationContext allows for re-refresh and re-close

Issue: SPR-13425
This commit is contained in:
Juergen Hoeller 2015-09-04 14:38:23 +02:00
parent c663fd551a
commit 811de8e50b
2 changed files with 20 additions and 13 deletions

View File

@ -567,6 +567,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
*/ */
protected void prepareRefresh() { protected void prepareRefresh() {
this.startupDate = System.currentTimeMillis(); this.startupDate = System.currentTimeMillis();
this.closed.set(false);
this.active.set(true); this.active.set(true);
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -49,7 +49,7 @@ import static org.junit.Assert.*;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
*/ */
public final class ClassPathXmlApplicationContextTests { public class ClassPathXmlApplicationContextTests {
private static final String PATH = "/org/springframework/context/support/"; private static final String PATH = "/org/springframework/context/support/";
private static final String RESOURCE_CONTEXT = PATH + "ClassPathXmlApplicationContextTests-resource.xml"; private static final String RESOURCE_CONTEXT = PATH + "ClassPathXmlApplicationContextTests-resource.xml";
@ -82,13 +82,23 @@ public final class ClassPathXmlApplicationContextTests {
@Test @Test
public void testMultipleConfigLocations() { public void testMultipleConfigLocations() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { FQ_CONTEXT_B, FQ_CONTEXT_C, FQ_CONTEXT_A}); FQ_CONTEXT_B, FQ_CONTEXT_C, FQ_CONTEXT_A);
assertTrue(ctx.containsBean("service")); assertTrue(ctx.containsBean("service"));
assertTrue(ctx.containsBean("logicOne")); assertTrue(ctx.containsBean("logicOne"));
assertTrue(ctx.containsBean("logicTwo")); assertTrue(ctx.containsBean("logicTwo"));
// re-refresh (after construction refresh)
Service service = (Service) ctx.getBean("service"); Service service = (Service) ctx.getBean("service");
ctx.refresh(); ctx.refresh();
assertTrue(service.isProperlyDestroyed()); assertTrue(service.isProperlyDestroyed());
// regular close call
service = (Service) ctx.getBean("service");
ctx.close();
assertTrue(service.isProperlyDestroyed());
// re-activating and re-closing the context (SPR-13425)
ctx.refresh();
service = (Service) ctx.getBean("service"); service = (Service) ctx.getBean("service");
ctx.close(); ctx.close();
assertTrue(service.isProperlyDestroyed()); assertTrue(service.isProperlyDestroyed());
@ -107,8 +117,7 @@ public final class ClassPathXmlApplicationContextTests {
@Test @Test
public void testSingleConfigLocationWithClass() { public void testSingleConfigLocationWithClass() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(SIMPLE_CONTEXT, getClass());
SIMPLE_CONTEXT, getClass());
assertTrue(ctx.containsBean("someMessageSource")); assertTrue(ctx.containsBean("someMessageSource"));
ctx.close(); ctx.close();
} }
@ -116,7 +125,7 @@ public final class ClassPathXmlApplicationContextTests {
@Test @Test
public void testAliasWithPlaceholder() { public void testAliasWithPlaceholder() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { FQ_CONTEXT_B, FQ_ALIASED_CONTEXT_C, FQ_CONTEXT_A}); FQ_CONTEXT_B, FQ_ALIASED_CONTEXT_C, FQ_CONTEXT_A);
assertTrue(ctx.containsBean("service")); assertTrue(ctx.containsBean("service"));
assertTrue(ctx.containsBean("logicOne")); assertTrue(ctx.containsBean("logicOne"));
assertTrue(ctx.containsBean("logicTwo")); assertTrue(ctx.containsBean("logicTwo"));
@ -144,16 +153,14 @@ public final class ClassPathXmlApplicationContextTests {
private void checkExceptionFromInvalidValueType(Throwable ex) throws IOException { private void checkExceptionFromInvalidValueType(Throwable ex) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
ex.printStackTrace(new PrintStream(baos)); ex.printStackTrace(new PrintStream(baos));
String dump = FileCopyUtils.copyToString( String dump = FileCopyUtils.copyToString(new InputStreamReader(new ByteArrayInputStream(baos.toByteArray())));
new InputStreamReader(new ByteArrayInputStream(baos.toByteArray())));
assertTrue(dump.contains("someMessageSource")); assertTrue(dump.contains("someMessageSource"));
assertTrue(dump.contains("useCodeAsDefaultMessage")); assertTrue(dump.contains("useCodeAsDefaultMessage"));
} }
@Test @Test
public void testContextWithInvalidLazyClass() { public void testContextWithInvalidLazyClass() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(INVALID_CLASS_CONTEXT, getClass());
INVALID_CLASS_CONTEXT, getClass());
assertTrue(ctx.containsBean("someMessageSource")); assertTrue(ctx.containsBean("someMessageSource"));
try { try {
ctx.getBean("someMessageSource"); ctx.getBean("someMessageSource");
@ -167,8 +174,7 @@ public final class ClassPathXmlApplicationContextTests {
@Test @Test
public void testContextWithClassNameThatContainsPlaceholder() { public void testContextWithClassNameThatContainsPlaceholder() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CLASS_WITH_PLACEHOLDER_CONTEXT, getClass());
CLASS_WITH_PLACEHOLDER_CONTEXT, getClass());
assertTrue(ctx.containsBean("someMessageSource")); assertTrue(ctx.containsBean("someMessageSource"));
assertTrue(ctx.getBean("someMessageSource") instanceof StaticMessageSource); assertTrue(ctx.getBean("someMessageSource") instanceof StaticMessageSource);
ctx.close(); ctx.close();
@ -282,7 +288,7 @@ public final class ClassPathXmlApplicationContextTests {
@Test @Test
public void testAliasThatOverridesEarlierBean() { public void testAliasThatOverridesEarlierBean() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] {FQ_SIMPLE_CONTEXT, ALIAS_THAT_OVERRIDES_PARENT_CONTEXT}); FQ_SIMPLE_CONTEXT, ALIAS_THAT_OVERRIDES_PARENT_CONTEXT);
Object myMs = ctx.getBean("myMessageSource"); Object myMs = ctx.getBean("myMessageSource");
Object someMs2 = ctx.getBean("someMessageSource"); Object someMs2 = ctx.getBean("someMessageSource");
assertSame(myMs, someMs2); assertSame(myMs, someMs2);