From d96a6914a8e82b726f8a35badf26a7713e83a17b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 3 Feb 2010 22:54:59 +0000 Subject: [PATCH] ApplicationListeners will get detected lazily as well (e.g. on @Bean's concrete result); inner bean ApplicationListeners will be invoked through their proxy (if any) --- .../ApplicationContextAwareProcessor.java | 45 +++++++++---------- .../ConfigurationClassProcessingTests.java | 19 +++++++- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/context/support/ApplicationContextAwareProcessor.java b/org.springframework.context/src/main/java/org/springframework/context/support/ApplicationContextAwareProcessor.java index 45f12067b3..3196066fe3 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/support/ApplicationContextAwareProcessor.java +++ b/org.springframework.context/src/main/java/org/springframework/context/support/ApplicationContextAwareProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,12 +73,12 @@ class ApplicationContextAwareProcessor implements MergedBeanDefinitionPostProces public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) { - if (!this.applicationContext.containsBean(beanName) && beanDefinition.isSingleton()) { + if (beanDefinition.isSingleton()) { this.singletonNames.put(beanName, Boolean.TRUE); } } - public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { + public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { AccessControlContext acc = null; if (System.getSecurityManager() != null && @@ -90,19 +90,19 @@ class ApplicationContextAwareProcessor implements MergedBeanDefinitionPostProces if (acc != null) { AccessController.doPrivileged(new PrivilegedAction() { public Object run() { - doProcess(bean, beanName); + invokeAwareInterfaces(bean); return null; } }, acc); } else { - doProcess(bean, beanName); + invokeAwareInterfaces(bean); } return bean; } - private void doProcess(Object bean, String beanName) { + private void invokeAwareInterfaces(Object bean) { if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext); } @@ -115,30 +115,27 @@ class ApplicationContextAwareProcessor implements MergedBeanDefinitionPostProces if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); } + } + public Object postProcessAfterInitialization(Object bean, String beanName) { if (bean instanceof ApplicationListener) { - if (!this.applicationContext.containsBean(beanName)) { - // not a top-level bean - not detected as a listener by getBeanNamesForType retrieval - Boolean flag = this.singletonNames.get(beanName); - if (Boolean.TRUE.equals(flag)) { - // inner singleton bean: register on the fly - this.applicationContext.addApplicationListener((ApplicationListener) bean); - } - else if (flag == null) { + // potentially not detected as a listener by getBeanNamesForType retrieval + Boolean flag = this.singletonNames.get(beanName); + if (Boolean.TRUE.equals(flag)) { + // singleton bean (top-level or inner): register on the fly + this.applicationContext.addApplicationListener((ApplicationListener) bean); + } + else if (flag == null) { + if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) { // inner bean with other scope - can't reliably process events - if (logger.isWarnEnabled()) { - logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " + - "but is not reachable for event multicasting by its containing ApplicationContext " + - "because it does not have singleton scope. Only top-level listener beans are allowed " + - "to be of non-singleton scope."); - } - this.singletonNames.put(beanName, Boolean.FALSE); + logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " + + "but is not reachable for event multicasting by its containing ApplicationContext " + + "because it does not have singleton scope. Only top-level listener beans are allowed " + + "to be of non-singleton scope."); } + this.singletonNames.put(beanName, Boolean.FALSE); } } - } - - public Object postProcessAfterInitialization(Object bean, String name) { return bean; } diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java index a0f734177e..94c931f3c2 100644 --- a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java +++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java @@ -32,12 +32,14 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigUtils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ConfigurationClassPostProcessor; import org.springframework.context.annotation.Scope; +import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.support.GenericApplicationContext; /** @@ -45,6 +47,7 @@ import org.springframework.context.support.GenericApplicationContext; * handling within {@link Configuration} class definitions. * * @author Chris Beams + * @author Juergen Hoeller */ public class ConfigurationClassProcessingTests { @@ -139,6 +142,9 @@ public class ConfigurationClassProcessingTests { assertEquals("foo-processed", foo.getName()); assertEquals("bar-processed", bar.getName()); assertEquals("baz-processed", baz.getName()); + + SpousyTestBean listener = factory.getBean("listenerTestBean", SpousyTestBean.class); + assertTrue(listener.refreshed); } @@ -232,10 +238,17 @@ public class ConfigurationClassProcessingTests { } }; } + + @Bean + public ITestBean listenerTestBean() { + return new SpousyTestBean("listener"); + } } - private static class SpousyTestBean extends TestBean { + private static class SpousyTestBean extends TestBean implements ApplicationListener { + + public boolean refreshed = false; public SpousyTestBean(String name) { super(name); @@ -246,6 +259,10 @@ public class ConfigurationClassProcessingTests { public void setSpouse(ITestBean spouse) { super.setSpouse(spouse); } + + public void onApplicationEvent(ContextRefreshedEvent event) { + this.refreshed = true; + } } } -- GitLab