diff --git a/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java index 9e3626e1d4..aff0017ef9 100644 --- a/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java @@ -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"); * you may not use this file except in compliance with the License. @@ -17,10 +17,12 @@ package org.springframework.context.support; import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; @@ -89,7 +91,7 @@ public class GenericApplicationContext extends AbstractApplicationContext implem private ResourceLoader resourceLoader; - private boolean refreshed = false; + private final AtomicBoolean refreshed = new AtomicBoolean(); /** @@ -235,12 +237,11 @@ public class GenericApplicationContext extends AbstractApplicationContext implem */ @Override protected final void refreshBeanFactory() throws IllegalStateException { - if (this.refreshed) { + if (!this.refreshed.compareAndSet(false, true)) { throw new IllegalStateException( "GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once"); } this.beanFactory.setSerializationId(getId()); - this.refreshed = true; } @Override @@ -279,6 +280,12 @@ public class GenericApplicationContext extends AbstractApplicationContext implem return this.beanFactory; } + @Override + public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException { + assertBeanFactoryActive(); + return this.beanFactory; + } + //--------------------------------------------------------------------- // Implementation of BeanDefinitionRegistry diff --git a/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java b/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java index abfd80b9bb..e215830d46 100644 --- a/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java +++ b/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -55,4 +55,34 @@ public class GenericApplicationContextTests { } } + @Test + public void accessAfterClosing() { + GenericApplicationContext ac = new GenericApplicationContext(); + ac.registerBeanDefinition("testBean", new RootBeanDefinition(String.class)); + ac.refresh(); + + assertSame(ac.getBean("testBean"), ac.getBean(String.class)); + assertSame(ac.getAutowireCapableBeanFactory().getBean("testBean"), + ac.getAutowireCapableBeanFactory().getBean(String.class)); + + ac.close(); + + try { + assertSame(ac.getBean("testBean"), ac.getBean(String.class)); + fail("Should have thrown IllegalStateException"); + } + catch (IllegalStateException ex) { + // expected + } + + try { + assertSame(ac.getAutowireCapableBeanFactory().getBean("testBean"), + ac.getAutowireCapableBeanFactory().getBean(String.class)); + fail("Should have thrown IllegalStateException"); + } + catch (IllegalStateException ex) { + // expected + } + } + }