mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-08 05:02:13 +00:00
Add ObjectPostProcessor support for SmartInitializingSingleton
This commit is contained in:
parent
6649d46896
commit
d002681bec
@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.Aware;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
import org.springframework.security.config.annotation.ObjectPostProcessor;
|
||||
import org.springframework.util.Assert;
|
||||
@ -37,10 +38,11 @@ import org.springframework.util.Assert;
|
||||
* @since 3.2
|
||||
*/
|
||||
final class AutowireBeanFactoryObjectPostProcessor
|
||||
implements ObjectPostProcessor<Object>, DisposableBean {
|
||||
implements ObjectPostProcessor<Object>, DisposableBean, SmartInitializingSingleton {
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
private final AutowireCapableBeanFactory autowireBeanFactory;
|
||||
private final List<DisposableBean> disposableBeans = new ArrayList<DisposableBean>();
|
||||
private final List<SmartInitializingSingleton> smartSingletons = new ArrayList<SmartInitializingSingleton>();
|
||||
|
||||
public AutowireBeanFactoryObjectPostProcessor(
|
||||
AutowireCapableBeanFactory autowireBeanFactory) {
|
||||
@ -74,9 +76,22 @@ final class AutowireBeanFactoryObjectPostProcessor
|
||||
if (result instanceof DisposableBean) {
|
||||
this.disposableBeans.add((DisposableBean) result);
|
||||
}
|
||||
if (result instanceof SmartInitializingSingleton) {
|
||||
this.smartSingletons.add((SmartInitializingSingleton) result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
|
||||
*/
|
||||
@Override
|
||||
public void afterSingletonsInstantiated() {
|
||||
for(SmartInitializingSingleton singleton : smartSingletons) {
|
||||
singleton.afterSingletonsInstantiated();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
|
@ -18,6 +18,7 @@ package org.springframework.security.config.annotation.configuration
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware
|
||||
import org.springframework.beans.factory.BeanFactoryAware
|
||||
import org.springframework.beans.factory.DisposableBean
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory
|
||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||
@ -138,4 +139,36 @@ class AutowireBeanFactoryObjectPostProcessorTests extends BaseSpringSpec {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user