<span style="background-color: rgb(255, 255, 255);">To prevent the methods be triggered twice, we need to overwrite the </span><strong style="background-color: rgb(255, 255, 255);">publishEvent(ApplicationEvent event)</strong><span style="background-color: rgb(255, 255, 255);"> method, then remove the code below</span>
<span style="white-space:pre"> </span>this.getApplicationEventMulticaster().multicastEvent(event);
if(this.parent != null) {
this.parent.publishEvent(event);
}
So, it need to create a customized AnnotationConfuigWebApplicationContext: package com.siemens.ct.its.iam.deploy.configuration;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.util.Assert;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by z002x71p on 2015/9/23.
*/
public class MyAnnotationConfigWebApplicationContext extends AnnotationConfigWebApplicationContext {
public void publishEvent(ApplicationEvent event) {
Assert.notNull(event, "Event must not be null");
if(this.logger.isTraceEnabled()) {
this.logger.trace("Publishing event in " + this.getDisplayName() + ": " + event);
}
this.getApplicationEventMulticaster().multicastEvent(event);
}
ApplicationEventMulticaster getApplicationEventMulticaster() throws IllegalStateException {
for(Class<?> clazz = this.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()) {
String className = clazz.getName();
System.out.println(className);
if(className.equals("org.springframework.context.support.AbstractApplicationContext")){
try {
Method method = clazz.getDeclaredMethod("getApplicationEventMulticaster") ;
method.setAccessible(true);
return (ApplicationEventMulticaster) method.invoke(this);
}catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e){
throw new RuntimeException(e);
}
}
}
return null;
}
}
And set it into the DispacherServlet.