Add ObjectPostProcessor support for SmartInitializingSingleton

This commit is contained in:
Rob Winch 2016-07-21 00:40:43 -05:00
parent 6649d46896
commit d002681bec
2 changed files with 49 additions and 1 deletions

View File

@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.Aware; import org.springframework.beans.factory.Aware;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.security.config.annotation.ObjectPostProcessor; import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -37,10 +38,11 @@ import org.springframework.util.Assert;
* @since 3.2 * @since 3.2
*/ */
final class AutowireBeanFactoryObjectPostProcessor final class AutowireBeanFactoryObjectPostProcessor
implements ObjectPostProcessor<Object>, DisposableBean { implements ObjectPostProcessor<Object>, DisposableBean, SmartInitializingSingleton {
private final Log logger = LogFactory.getLog(getClass()); private final Log logger = LogFactory.getLog(getClass());
private final AutowireCapableBeanFactory autowireBeanFactory; private final AutowireCapableBeanFactory autowireBeanFactory;
private final List<DisposableBean> disposableBeans = new ArrayList<DisposableBean>(); private final List<DisposableBean> disposableBeans = new ArrayList<DisposableBean>();
private final List<SmartInitializingSingleton> smartSingletons = new ArrayList<SmartInitializingSingleton>();
public AutowireBeanFactoryObjectPostProcessor( public AutowireBeanFactoryObjectPostProcessor(
AutowireCapableBeanFactory autowireBeanFactory) { AutowireCapableBeanFactory autowireBeanFactory) {
@ -74,9 +76,22 @@ final class AutowireBeanFactoryObjectPostProcessor
if (result instanceof DisposableBean) { if (result instanceof DisposableBean) {
this.disposableBeans.add((DisposableBean) result); this.disposableBeans.add((DisposableBean) result);
} }
if (result instanceof SmartInitializingSingleton) {
this.smartSingletons.add((SmartInitializingSingleton) result);
}
return result; return result;
} }
/* (non-Javadoc)
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
*/
@Override
public void afterSingletonsInstantiated() {
for(SmartInitializingSingleton singleton : smartSingletons) {
singleton.afterSingletonsInstantiated();
}
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* *

View File

@ -18,6 +18,7 @@ package org.springframework.security.config.annotation.configuration
import org.springframework.beans.factory.BeanClassLoaderAware import org.springframework.beans.factory.BeanClassLoaderAware
import org.springframework.beans.factory.BeanFactoryAware import org.springframework.beans.factory.BeanFactoryAware
import org.springframework.beans.factory.DisposableBean import org.springframework.beans.factory.DisposableBean
import org.springframework.beans.factory.SmartInitializingSingleton
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.config.AutowireCapableBeanFactory import org.springframework.beans.factory.config.AutowireCapableBeanFactory
import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.BeanNameGenerator;
@ -138,4 +139,36 @@ class AutowireBeanFactoryObjectPostProcessorTests extends BaseSpringSpec {
p.postProcess(new Object()) p.postProcess(new Object())
} }
} }
def "SmartInitializingSingleton"() {
when:
context = new AnnotationConfigWebApplicationContext([servletConfig:new MockServletConfig(),servletContext:new MockServletContext()])
context.register(SmartConfig)
context.refresh()
context.start()
then:
context.getBean(SmartConfig).smart.instantiated
}
@Configuration
static class SmartConfig {
SmartInitializingSingleton smart = new SmartInitializingSingletonStub()
@Bean
public static ObjectPostProcessor objectPostProcessor(AutowireCapableBeanFactory beanFactory) {
return new AutowireBeanFactoryObjectPostProcessor(beanFactory)
}
@Autowired
public void configure(ObjectPostProcessor<Object> p) {
p.postProcess(smart)
}
}
static class SmartInitializingSingletonStub implements SmartInitializingSingleton {
boolean instantiated
void afterSingletonsInstantiated() {
instantiated = true
}
}
} }