1 16 17 package org.springframework.context.event; 18 19 import java.lang.reflect.Constructor ; 20 21 import org.aopalliance.intercept.MethodInterceptor; 22 import org.aopalliance.intercept.MethodInvocation; 23 24 import org.springframework.beans.factory.InitializingBean; 25 import org.springframework.context.ApplicationEvent; 26 import org.springframework.context.ApplicationEventPublisher; 27 import org.springframework.context.ApplicationEventPublisherAware; 28 29 48 public class EventPublicationInterceptor 49 implements MethodInterceptor, ApplicationEventPublisherAware, InitializingBean { 50 51 private Constructor applicationEventClassConstructor; 52 53 private ApplicationEventPublisher applicationEventPublisher; 54 55 56 65 public void setApplicationEventClass(Class applicationEventClass) { 66 if (ApplicationEvent.class.equals(applicationEventClass) || 67 !ApplicationEvent.class.isAssignableFrom(applicationEventClass)) { 68 throw new IllegalArgumentException ("applicationEventClass needs to extend ApplicationEvent"); 69 } 70 try { 71 this.applicationEventClassConstructor = 72 applicationEventClass.getConstructor(new Class [] {Object .class}); 73 } 74 catch (NoSuchMethodException ex) { 75 throw new IllegalArgumentException ("applicationEventClass [" + 76 applicationEventClass.getName() + "] does not have the required Object constructor: " + ex); 77 } 78 } 79 80 public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { 81 this.applicationEventPublisher = applicationEventPublisher; 82 } 83 84 public void afterPropertiesSet() throws Exception { 85 if (this.applicationEventClassConstructor == null) { 86 throw new IllegalArgumentException ("applicationEventClass is required"); 87 } 88 } 89 90 91 public Object invoke(MethodInvocation invocation) throws Throwable { 92 Object retVal = invocation.proceed(); 93 94 ApplicationEvent event = (ApplicationEvent) 95 this.applicationEventClassConstructor.newInstance(new Object [] {invocation.getThis()}); 96 this.applicationEventPublisher.publishEvent(event); 97 98 return retVal; 99 } 100 101 } 102 | Popular Tags |