refined addApplicationListener to work any time during the refresh phase

This commit is contained in:
Juergen Hoeller 2010-02-03 23:09:42 +00:00
parent d96a6914a8
commit ce6f14bd8b
1 changed files with 7 additions and 2 deletions

View File

@ -365,8 +365,8 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
} }
public void addApplicationListener(ApplicationListener listener) { public void addApplicationListener(ApplicationListener listener) {
if (isActive()) { if (this.applicationEventMulticaster != null) {
addListener(listener); this.applicationEventMulticaster.addApplicationListener(listener);
} }
else { else {
this.applicationListeners.add(listener); this.applicationListeners.add(listener);
@ -838,8 +838,13 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
/** /**
* Subclasses can invoke this method to register a listener. * Subclasses can invoke this method to register a listener.
* Any beans in the context that are listeners are automatically added. * Any beans in the context that are listeners are automatically added.
* <p>Note: This method only works within an active application context,
* i.e. when an ApplicationEventMulticaster is already available. Generally
* prefer the use of {@link #addApplicationListener} which is more flexible.
* @param listener the listener to register * @param listener the listener to register
* @deprecated as of Spring 3.0, in favor of {@link #addApplicationListener}
*/ */
@Deprecated
protected void addListener(ApplicationListener listener) { protected void addListener(ApplicationListener listener) {
getApplicationEventMulticaster().addApplicationListener(listener); getApplicationEventMulticaster().addApplicationListener(listener);
} }