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)
This commit is contained in:
parent
4d897e7ab5
commit
d96a6914a8
|
|
@ -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<Object>() {
|
||||
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,18 +115,19 @@ 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
|
||||
// potentially 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
|
||||
// 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 " +
|
||||
|
|
@ -135,10 +136,6 @@ class ApplicationContextAwareProcessor implements MergedBeanDefinitionPostProces
|
|||
this.singletonNames.put(beanName, Boolean.FALSE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Object postProcessAfterInitialization(Object bean, String name) {
|
||||
return bean;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<ContextRefreshedEvent> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue