Explorar o código

Add ObjectPostProcessor support for SmartInitializingSingleton

Rob Winch %!s(int64=9) %!d(string=hai) anos
pai
achega
d002681bec

+ 16 - 1
config/src/main/java/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessor.java

@@ -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)
 	 *

+ 33 - 0
config/src/test/groovy/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests.groovy

@@ -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
+		}
+	}
 }