成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring容器啟動過程中發布的核心事件及事件處理機制詳解

開發 項目管理
到Spring 4.2為止,事件基礎設施得到了顯著改進,提供了基于注釋的模型以及發布任意事件的能力(也就是說,不一定是從ApplicationEvent擴展的對象)。當這樣的對象被發布時,我們將它包裝在一個事件中。

核心事件

ApplicationContext中的事件處理是通過ApplicationEvent類和ApplicationListener接口提供的。如果將實現一個Bean實現了ApplicationListener接口,那么每當ApplicationEvent發布到ApplicationContext時,就會通知該bean。本質上,這是標準的觀察者設計模式。

到Spring 4.2為止,事件基礎設施得到了顯著改進,提供了基于注釋的模型以及發布任意事件的能力(也就是說,不一定是從ApplicationEvent擴展的對象)。當這樣的對象被發布時,我們將它包裝在一個事件中。

下表列出了Spring提供的標準事件:

Event

Explanation

ContextRefreshedEvent

在ApplicationContext被初始化或刷新時發布(例如,通過使用ConfigurableApplicationContext接口上的refresh()方法)。這里的“初始化”意味著加載了所有bean,檢測并激活了后處理器bean,預實例化了單例,并且ApplicationContext對象已經準備好可以使用。只要上下文沒有關閉,就可以多次觸發刷新,前提是所選擇的ApplicationContext實際上支持這種“熱”刷新。

ContextStartedEvent

通過使用ConfigurableApplicationContext接口上的start()方法啟動ApplicationContext時發布。在這里,“started”意味著所有生命周期bean都接收一個顯式的開始信號。通常,該信號用于在顯式停止之后重新啟動bean,但也可以用于啟動未配置為自動啟動的組件(例如,在初始化時尚未啟動的組件)。

ContextStoppedEvent

當通過使用ConfigurableApplicationContext接口上的stop()方法停止ApplicationContext時發布。在這里,“stopped”意味著所有生命周期bean都接收一個明確的停止信號。停止的上下文可以通過start()調用重新啟動。

ContextClosedEvent

使用ConfigurableApplicationContext接口上的close()方法或通過JVM關閉掛鉤關閉ApplicationContext時發布。在這里,“closed”意味著所有的單例bean都會被銷毀。一旦上下文被關閉,它就會到達生命的終點,并且不能被刷新或重啟。

RequestHandledEvent

一個特定于web的事件,告訴所有bean一個HTTP請求已經得到了服務。此事件在請求完成后發布。這個事件只適用于使用Spring的DispatcherServlet的web應用程序。

ServletRequestHandledEvent

RequestHandledEvent的一個子類,用于添加特定于servlet的上下文信息。

以上事件發布時機:

  • ContextRefreshedEvent

public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {
public void refresh() {
// ...
finishRefresh();
}
}

  • ContextStartedEvent

public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {
public void refresh() {
// ...
finishRefresh();
}
protected void finishRefresh() {
// 初始化LifecycleProcessor(DefaultLifecycleProcessor)
initLifecycleProcessor();
getLifecycleProcessor().onRefresh();
}
public void start() {
getLifecycleProcessor().start();
publishEvent(new ContextStartedEvent(this));
}
}
public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactoryAware {
public void start() {
startBeans(false);
this.running = true;
}
}

  • ContextStoppedEvent

該事件與上面的started是對應的

public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {
public void stop() {
getLifecycleProcessor().stop();
publishEvent(new ContextStoppedEvent(this));
}
}
public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactoryAware {
public void stop() {
stopBeans();
this.running = false;
}
}

  • ContextClosedEvent

public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {
public void close() {
synchronized (this.startupShutdownMonitor) {
doClose();
}
}
protected void doClose() {
publishEvent(new ContextClosedEvent(this));
}
}

  • ServletRequestHandledEvent

public abstract class FrameworkServlet {
protected final void processRequest(HttpServletRequest request, HttpServletResponse response) {
publishRequestHandledEvent(request, response, startTime, failureCause);
}
private void publishRequestHandledEvent(HttpServletRequest request, HttpServletResponse response, long startTime, @Nullable Throwable failureCause) {
if (this.publishEvents && this.webApplicationContext != null) {
// Whether or not we succeeded, publish an event.
long processingTime = System.currentTimeMillis() - startTime;
this.webApplicationContext.publishEvent(
new ServletRequestHandledEvent(this,
request.getRequestURI(), request.getRemoteAddr(),
request.getMethod(), getServletConfig().getServletName(),
WebUtils.getSessionId(request), getUsernameForRequest(request),
processingTime, failureCause, response.getStatus()));
}
}
}

你還可以創建和發布自己的自定義事件。下面的例子展示了一個簡單的類,它擴展了Spring的ApplicationEvent基類:

public class BlockedListEvent extends ApplicationEvent {


private final String address;
private final String content;


public BlockedListEvent(Object source, String address, String content) {
super(source);
this.address = address;
this.content = content;
}
}

要發布自定義的ApplicationEvent,需要調用ApplicationEventPublisher的publishEvent()方法。通常,這是通過創建一個實現ApplicationEventPublisherAware的類并將其注冊為Spring bean來完成的。下面的例子展示了這樣一個類:

public class EmailService implements ApplicationEventPublisherAware {


private List<String> blockedList;
private ApplicationEventPublisher publisher;


public void setBlockedList(List<String> blockedList) {
this.blockedList = blockedList;
}


public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}


public void sendEmail(String address, String content) {
if (blockedList.contains(address)) {
publisher.publishEvent(new BlockedListEvent(this, address, content));
return;
}
}
}

在配置時,Spring容器檢測到EmailService實現了ApplicationEventPublisherAware,并自動調用
setApplicationEventPublisher()。實際上,傳入的參數是Spring容器本身。通過ApplicationEventPublisher接口與應用程序上下文進行交互。

要接收自定義的ApplicationEvent,可以創建一個實現ApplicationListener的類,并將其注冊為Spring bean。下面的例子展示了這樣一個類:

public class BlockedListNotifier implements ApplicationListener<BlockedListEvent> {


private String notificationAddress;


public void setNotificationAddress(String notificationAddress) {
this.notificationAddress = notificationAddress;
}


public void onApplicationEvent(BlockedListEvent event) {
}
}

請注意,ApplicationListener通常參數化為自定義事件的類型。這意味著onApplicationEvent()方法可以保持類型安全,避免任何向下轉換的需要。你可以注冊任意數量的事件監聽器,但請注意,默認情況下,事件監聽器是同步接收事件的。這意味著publishEvent()方法會阻塞,直到所有監聽器都完成事件處理。這種同步和單線程方法的一個優點是,當偵聽器接收到事件時,如果事務上下文可用,它將在發布者的事務上下文內操作。

通過注解監聽事件

可以使用@EventListener注解在托管bean的任何方法上注冊事件監聽器。可以將BlockedListNotifier重寫為:

public class BlockedListNotifier {


private String notificationAddress;


public void setNotificationAddress(String notificationAddress) {
this.notificationAddress = notificationAddress;
}


@EventListener
public void processBlockedListEvent(BlockedListEvent event) {
}
}

同時監聽多個事件

@EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class})
public void handleContextStart() {
}

異步事件

如果希望特定的監聽器異步處理事件,可以重用常規的@Async支持。如下面的例子所示:

@EventListener
@Async
public void processBlockedListEvent(BlockedListEvent event) {
}

在使用異步事件時,請注意以下限制:

  1. 如果異步事件監聽器拋出異常,則異常不會傳播到調用者。
  2. 異步事件監聽器方法無法通過返回值發布后續事件。如果你需要發布另一個事件作為處理的結果,注入一個ApplicationEventPublisher來手動發布事件。

事件監聽順序

如果需要在調用另一個監聽器之前調用一個監聽器,可以在方法聲明中添加@Order注解,如下面的例子所示:

@EventListener
@Order(1)
public void processBlockedListEvent(BlockedListEvent event) {
}

通用的事件

你還可以使用泛型來進一步定義事件的結構。考慮使用EntityCreatedEvent<T>,其中T是實際創建的實體的類型。例如,你可以創建以下監聽器定義,只接收Person的EntityCreatedEvent:

@EventListener
public void onPersonCreated(EntityCreatedEvent<Person> event) {
}

由于類型擦除,只有當觸發的事件解析了事件監聽器過濾的泛型參數(即類似于PersonCreatedEvent類擴展EntityCreatedEvent<Person>{…})時,才會起作用。

事件觸發原理

方式1:ApplicationEventPublisher

AbstractApplicationContext實現了ApplicationEventPublisher接口,那么只要ApplicationContext繼承自AbstractApplicationContext都可以直接發布事件:

public abstract class AbstractApplicationContext {
protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
ApplicationEvent applicationEvent;
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent) event;
} else {
applicationEvent = new PayloadApplicationEvent<>(this, event);
if (eventType == null) {
eventType = ((PayloadApplicationEvent<?>) applicationEvent).getResolvableType();
}
}
if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
} else {
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
}
if (this.parent != null) {
if (this.parent instanceof AbstractApplicationContext) {
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
} else {
this.parent.publishEvent(event);
}
}
}
}

方式2:通過@EventListener注解

該注解是由EventListenerMethodProcessor處理器處理的。

public class EventListenerMethodProcessor implements SmartInitializingSingleton, ApplicationContextAware, BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// 獲取容器中注冊的事件監聽工廠,可以有多個
Map<String, EventListenerFactory> beans = beanFactory.getBeansOfType(EventListenerFactory.class, false, false);
}
public void afterSingletonsInstantiated() {
String[] beanNames = beanFactory.getBeanNamesForType(Object.class);
for (String beanName : beanNames) {
processBean(beanName, type);
}
}
private void processBean(final String beanName, final Class<?> targetType) {
Map<Method, EventListener> annotatedMethods = null;
try {
// 取得Bean內帶有@EventListener注解的方法
annotatedMethods = MethodIntrospector.selectMethods(targetType, (MethodIntrospector.MetadataLookup<EventListener>) method ->
AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class));
}
// 遍歷找到的所有@EventListener注解的方法
for (Method method : annotatedMethods.keySet()) {
// 遍歷所有的EventListenerFactory
for (EventListenerFactory factory : factories) {
// 判斷當前的事件監聽工廠是否支持當前的方法
if (factory.supportsMethod(method)) {
Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName));
// 創建對應的事件監聽程序
ApplicationListener<?> applicationListener = factory.createApplicationListener(beanName, targetType, methodToUse);
if (applicationListener instanceof ApplicationListenerMethodAdapter) {
((ApplicationListenerMethodAdapter) applicationListener).init(context, this.evaluator);
}
context.addApplicationListener(applicationListener);
break;
}
}
}
}
}


責任編輯:武曉燕 來源: 實戰案例錦集
相關推薦

2009-09-02 18:34:28

C#鼠標事件

2011-07-01 14:20:59

Qt 事件

2011-07-01 14:14:34

Qt 事件

2023-02-23 08:15:33

Spring異常處理機制

2011-03-17 09:20:05

異常處理機制

2023-06-15 14:09:00

解析器Servlet容器

2011-09-05 17:35:18

MTK啟動過程RTOS

2013-08-07 14:48:00

HTML5

2010-09-16 09:37:21

JavaScript事

2011-06-28 13:27:13

ARM Linux

2023-09-07 10:31:27

2010-03-05 15:40:16

Python異常

2023-09-14 15:15:36

2015-11-06 13:59:01

JavaScript事件處理

2021-09-28 15:03:06

Linux內核arm

2011-07-04 14:38:43

QT Qevent

2009-08-04 13:53:58

C#委托類C#事件

2017-01-11 18:44:43

React Nativ觸摸事件Android

2021-03-02 09:12:25

Java異常機制

2019-05-27 14:43:49

Tomcat架構部署
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲欧美高清 | 国产成人a亚洲精品 | aaaaaa大片免费看最大的 | 欧美一二三四成人免费视频 | 四色成人av永久网址 | 成人免费激情视频 | 国产精品欧美一区二区三区 | 在线视频日韩精品 | 欧美日韩综合精品 | 欧美老少妇一级特黄一片 | 国产高清区 | 国产亚洲欧美另类一区二区三区 | 久久中文字幕一区 | 国产精品久久久久久久一区探花 | 久久99国产精一区二区三区 | 91麻豆产精品久久久久久 | 精品区一区二区 | 欧美在线一区二区视频 | 精品亚洲视频在线 | 欧美女优在线观看 | 国产成人av电影 | 欧美色a v| 国产一级免费视频 | 日日欧美 | 激情视频网站 | 日韩在线| 国产黄色大片在线观看 | 日韩性生活网 | 亚洲欧美精品国产一级在线 | 草久久| 国产精品久久久久久久7电影 | 最新高清无码专区 | 色婷婷亚洲国产女人的天堂 | 国产精品激情小视频 | 日本不卡一区二区三区在线观看 | 亚洲精品日韩精品 | 成人一区二区三区在线观看 | 日日天天 | 精品亚洲一区二区三区 | 国产精品永久免费视频 | 奇米影视在线 |