Skip to content

Commit 706d3ad

Browse files
committed
Consistently throw IllegalStateException from getAutowireCapableBeanFactory()
Issue: SPR-12932
1 parent 91e46cf commit 706d3ad

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -17,10 +17,12 @@
1717
package org.springframework.context.support;
1818

1919
import java.io.IOException;
20+
import java.util.concurrent.atomic.AtomicBoolean;
2021

2122
import org.springframework.beans.BeansException;
2223
import org.springframework.beans.factory.BeanDefinitionStoreException;
2324
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
25+
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
2426
import org.springframework.beans.factory.config.BeanDefinition;
2527
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2628
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -89,7 +91,7 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
8991

9092
private ResourceLoader resourceLoader;
9193

92-
private boolean refreshed = false;
94+
private final AtomicBoolean refreshed = new AtomicBoolean();
9395

9496

9597
/**
@@ -235,12 +237,11 @@ public Resource[] getResources(String locationPattern) throws IOException {
235237
*/
236238
@Override
237239
protected final void refreshBeanFactory() throws IllegalStateException {
238-
if (this.refreshed) {
240+
if (!this.refreshed.compareAndSet(false, true)) {
239241
throw new IllegalStateException(
240242
"GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
241243
}
242244
this.beanFactory.setSerializationId(getId());
243-
this.refreshed = true;
244245
}
245246

246247
@Override
@@ -279,6 +280,12 @@ public final DefaultListableBeanFactory getDefaultListableBeanFactory() {
279280
return this.beanFactory;
280281
}
281282

283+
@Override
284+
public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
285+
assertBeanFactoryActive();
286+
return this.beanFactory;
287+
}
288+
282289

283290
//---------------------------------------------------------------------
284291
// Implementation of BeanDefinitionRegistry

spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -55,4 +55,34 @@ public void getBeanForClass() {
5555
}
5656
}
5757

58+
@Test
59+
public void accessAfterClosing() {
60+
GenericApplicationContext ac = new GenericApplicationContext();
61+
ac.registerBeanDefinition("testBean", new RootBeanDefinition(String.class));
62+
ac.refresh();
63+
64+
assertSame(ac.getBean("testBean"), ac.getBean(String.class));
65+
assertSame(ac.getAutowireCapableBeanFactory().getBean("testBean"),
66+
ac.getAutowireCapableBeanFactory().getBean(String.class));
67+
68+
ac.close();
69+
70+
try {
71+
assertSame(ac.getBean("testBean"), ac.getBean(String.class));
72+
fail("Should have thrown IllegalStateException");
73+
}
74+
catch (IllegalStateException ex) {
75+
// expected
76+
}
77+
78+
try {
79+
assertSame(ac.getAutowireCapableBeanFactory().getBean("testBean"),
80+
ac.getAutowireCapableBeanFactory().getBean(String.class));
81+
fail("Should have thrown IllegalStateException");
82+
}
83+
catch (IllegalStateException ex) {
84+
// expected
85+
}
86+
}
87+
5888
}

0 commit comments

Comments
 (0)