Consistently throw IllegalStateException from getAutowireCapableBeanFactory()
Issue: SPR-12932
This commit is contained in:
parent
91e46cf2ad
commit
706d3adf07
|
|
@ -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.
|
||||||
|
|
@ -17,10 +17,12 @@
|
||||||
package org.springframework.context.support;
|
package org.springframework.context.support;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
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.BeanDefinition;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
|
|
@ -89,7 +91,7 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
|
||||||
|
|
||||||
private ResourceLoader resourceLoader;
|
private ResourceLoader resourceLoader;
|
||||||
|
|
||||||
private boolean refreshed = false;
|
private final AtomicBoolean refreshed = new AtomicBoolean();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -235,12 +237,11 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected final void refreshBeanFactory() throws IllegalStateException {
|
protected final void refreshBeanFactory() throws IllegalStateException {
|
||||||
if (this.refreshed) {
|
if (!this.refreshed.compareAndSet(false, true)) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
|
"GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
|
||||||
}
|
}
|
||||||
this.beanFactory.setSerializationId(getId());
|
this.beanFactory.setSerializationId(getId());
|
||||||
this.refreshed = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -279,6 +280,12 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
|
||||||
return this.beanFactory;
|
return this.beanFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
|
||||||
|
assertBeanFactoryActive();
|
||||||
|
return this.beanFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// Implementation of BeanDefinitionRegistry
|
// Implementation of BeanDefinitionRegistry
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* 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.
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue