From 8af7f279428e6b07d7251e67b5c68e92e4500481 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 7 Jan 2010 12:58:18 +0000 Subject: [PATCH] ApplicationListeners will only be executed once per event (even for scripted objects; SPR-6589) --- .../AbstractApplicationEventMulticaster.java | 4 ++-- .../scripting/bsh/BshScriptFactoryTests.java | 18 +++++++++++++++++- .../scripting/bsh/bsh-with-xsd.xml | 8 ++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java b/org.springframework.context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java index 3bba5a6360..8a1bd763f0 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java +++ b/org.springframework.context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.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. @@ -146,7 +146,7 @@ public abstract class AbstractApplicationEventMulticaster implements Application BeanFactory beanFactory = getBeanFactory(); for (String listenerBeanName : this.defaultRetriever.applicationListenerBeans) { ApplicationListener listener = beanFactory.getBean(listenerBeanName, ApplicationListener.class); - if (supportsEvent(listener, eventType, sourceType)) { + if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) { retriever.applicationListenerBeans.add(listenerBeanName); allListeners.add(listener); } diff --git a/org.springframework.context/src/test/java/org/springframework/scripting/bsh/BshScriptFactoryTests.java b/org.springframework.context/src/test/java/org/springframework/scripting/bsh/BshScriptFactoryTests.java index 090ef9fde4..d3ecec1760 100644 --- a/org.springframework.context/src/test/java/org/springframework/scripting/bsh/BshScriptFactoryTests.java +++ b/org.springframework.context/src/test/java/org/springframework/scripting/bsh/BshScriptFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 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. @@ -26,6 +26,7 @@ import org.springframework.aop.support.AopUtils; import org.springframework.aop.target.dynamic.Refreshable; import org.springframework.beans.TestBean; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEvent; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.NestedRuntimeException; import org.springframework.scripting.Calculator; @@ -303,4 +304,19 @@ public class BshScriptFactoryTests extends TestCase { assertTrue("Messenger should be Refreshable", messenger instanceof Refreshable); } + public void testApplicationEventListener() throws Exception { + ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass()); + Messenger eventListener = (Messenger) ctx.getBean("eventListener"); + ctx.publishEvent(new MyEvent(ctx)); + assertEquals("count=2", eventListener.getMessage()); + } + + + private static class MyEvent extends ApplicationEvent { + + public MyEvent(Object source) { + super(source); + } + } + } diff --git a/org.springframework.context/src/test/java/org/springframework/scripting/bsh/bsh-with-xsd.xml b/org.springframework.context/src/test/java/org/springframework/scripting/bsh/bsh-with-xsd.xml index 09453b3c0d..5908749d75 100644 --- a/org.springframework.context/src/test/java/org/springframework/scripting/bsh/bsh-with-xsd.xml +++ b/org.springframework.context/src/test/java/org/springframework/scripting/bsh/bsh-with-xsd.xml @@ -52,4 +52,12 @@ + + + +