ApplicationListener是對application生命週期的監聽體制。java
ApplicationEventMulticaster只有一個實現類SimpleApplicationEventMulticasterweb
ApplicationListener對象繼承了java.util.EventListener。ApplicationEvent繼承了java.util.EventObject。spring的ApplicationListener體系是基於java的event體制實現,並在上面進行擴展。spring
public interface ApplicationEventMulticaster { void addApplicationListener(ApplicationListener<?> listener); void addApplicationListenerBean(String listenerBeanName); void removeApplicationListener(ApplicationListener<?> listener); void removeApplicationListenerBean(String listenerBeanName); void removeAllListeners(); void multicastEvent(ApplicationEvent event); void multicastEvent(ApplicationEvent event, ResolvableType eventType); }
事件處理器管理類,負責ApplicationListener的管理,事件的多播。multicastEvent方法是接受事件(ApplicationEvent)調用對應ApplicationListener緩存
AbstractApplicationContextapp
public void addApplicationListener(ApplicationListener<?> listener) { Assert.notNull(listener, "ApplicationListener must not be null"); if (this.applicationEventMulticaster != null) { this.applicationEventMulticaster.addApplicationListener(listener); } else { this.applicationListeners.add(listener); } }
ApplicationListenerDetector異步
public Object postProcessAfterInitialization(Object bean, String beanName) { if (this.applicationContext != null && bean instanceof ApplicationListener) { // potentially not detected as a listener by getBeanNamesForType retrieval Boolean flag = this.singletonNames.get(beanName); if (Boolean.TRUE.equals(flag)) { // singleton bean (top-level or inner): register on the fly this.applicationContext.addApplicationListener((ApplicationListener<?>) bean); } else if (Boolean.FALSE.equals(flag)) { if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) { // inner bean with other scope - can't reliably process events 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 " + "to be of non-singleton scope."); } this.singletonNames.remove(beanName); } } return bean; }
public @interface EventListener { @AliasFor("classes") Class<?>[] value() default {}; @AliasFor("value") Class<?>[] classes() default {}; String condition() default ""; }
EventListenerMethodProcessoride
protected void processBean(final List<EventListenerFactory> factories, final String beanName, final Class<?> targetType) { if (!this.nonAnnotatedClasses.contains(targetType)) { Map<Method, EventListener> annotatedMethods = null; try { annotatedMethods = MethodIntrospector.selectMethods(targetType, new MethodIntrospector.MetadataLookup<EventListener>() { @Override public EventListener inspect(Method method) { return AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class); } }); } catch (Throwable ex) { // An unresolvable type in a method signature, probably from a lazy bean - let's ignore it. if (logger.isDebugEnabled()) { logger.debug("Could not resolve methods for bean with name '" + beanName + "'", ex); } } if (CollectionUtils.isEmpty(annotatedMethods)) { this.nonAnnotatedClasses.add(targetType); if (logger.isTraceEnabled()) { logger.trace("No @EventListener annotations found on bean class: " + targetType); } } else { // Non-empty set of methods for (Method method : annotatedMethods.keySet()) { for (EventListenerFactory factory : factories) { if (factory.supportsMethod(method)) { Method methodToUse = AopUtils.selectInvocableMethod( method, this.applicationContext.getType(beanName)); ApplicationListener<?> applicationListener = factory.createApplicationListener(beanName, targetType, methodToUse); if (applicationListener instanceof ApplicationListenerMethodAdapter) { ((ApplicationListenerMethodAdapter) applicationListener) .init(this.applicationContext, this.evaluator); } this.applicationContext.addApplicationListener(applicationListener); break; } } } if (logger.isDebugEnabled()) { logger.debug(annotatedMethods.size() + " @EventListener methods processed on bean '" + beanName + "': " + annotatedMethods); } } } }
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { void onApplicationEvent(E event); }
事件處理器,只負責處理ApplicationEvent的子類執行。post
public interface GenericApplicationListener extends ApplicationListener<ApplicationEvent>, Ordered { boolean supportsEventType(ResolvableType eventType); boolean supportsSourceType(Class<?> sourceType); }
GenericApplicationListener是ApplicationListener的子接口。主要功能是主動提供監控的事件。而不是像ApplicationListener經過類聲明中泛型獲得事件。同時提供GenericApplicationListener的實現類GenericApplicationListenerAdapter統一GenericApplicationListener與ApplicationListener的行爲this
| ------------ | ------------ | | | | | | |lua
public abstract class ApplicationEvent extends EventObject { private static final long serialVersionUID = 7099057708183571937L; private final long timestamp; public ApplicationEvent(Object source) { super(source); this.timestamp = System.currentTimeMillis(); } public final long getTimestamp() { return this.timestamp; } }
事件類型目前分ApplicationContextEvent與RequestHandledEvent兩大類。ApplicationContextEvent是對應應用上下文的事件,與RequestHandledEvent對應http服務事件。
ApplicationContextEvent
RequestHandledEvent
protected void finishRefresh() { // Initialize lifecycle processor for this context. initLifecycleProcessor(); // Propagate refresh to lifecycle processor first. getLifecycleProcessor().onRefresh(); // Publish the final event. publishEvent(new ContextRefreshedEvent(this)); // Participate in LiveBeansView MBean, if active. LiveBeansView.registerApplicationContext(this); }
protected void doClose() { if (this.active.get() && this.closed.compareAndSet(false, true)) { if (logger.isInfoEnabled()) { logger.info("Closing " + this); } LiveBeansView.unregisterApplicationContext(this); try { // Publish shutdown event. publishEvent(new ContextClosedEvent(this)); }catch (Throwable ex) { logger.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex); } // Stop all Lifecycle beans, to avoid delays during individual destruction. try { getLifecycleProcessor().onClose(); }catch (Throwable ex) { logger.warn("Exception thrown from LifecycleProcessor on context close", ex); } // Destroy all cached singletons in the context's BeanFactory. destroyBeans(); // Close the state of this context itself. closeBeanFactory(); // Let subclasses do some final clean-up if they wish... onClose(); this.active.set(false); } }
public void start() { getLifecycleProcessor().start(); publishEvent(new ContextStartedEvent(this)); }
@Override public void stop() { getLifecycleProcessor().stop(); publishEvent(new ContextStoppedEvent(this)); }
public interface ApplicationEventPublisher { void publishEvent(ApplicationEvent event); void publishEvent(Object event); }
你們會奇怪已經有處理類了,爲何須要一個ApplicationEventPublisher接口。application有屬於本身的行文操做,這些操做屬於application的。有哪些行爲操做請看下面的代碼
protected void publishEvent(Object event, ResolvableType eventType) { ApplicationEvent applicationEvent; // 判斷是不是ApplicationEvent if (event instanceof ApplicationEvent) { applicationEvent = (ApplicationEvent) event; } else { // 不是進行一次封裝 applicationEvent = new PayloadApplicationEvent<Object>(this, event); if (eventType == null) { eventType = ((PayloadApplicationEvent)applicationEvent).getResolvableType(); } } // 在spring啓動的的時候,發生的事件都會保存在earlyApplicationEvents。等啓動成功以後,會被所有執行 if (this.earlyApplicationEvents != null) { this.earlyApplicationEvents.add(applicationEvent); } else { getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType); } // 若是有parent容器,讓parent執行一次 if (this.parent != null) { if (this.parent instanceof AbstractApplicationContext) { ((AbstractApplicationContext) this.parent).publishEvent(event, eventType); } else { this.parent.publishEvent(event); } } }