日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

中國最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2

wkspring教程

Spring 中的事件處理

閱讀 (2226)

Spring 中的事件處理

你已經看到了在所有章節中 Spring 的核心是 ApplicationContext,它負責管理 beans 的完整生命周期。當加載 beans 時,ApplicationContext 發布某些類型的事件。例如,當上下文啟動時,ContextStartedEvent 發布,當上下文停止時,ContextStoppedEvent 發布。

通過 ApplicationEvent 類和 ApplicationListener 接口來提供在 ApplicationContext 中處理事件。如果一個 bean 實現 ApplicationListener,那么每次 ApplicationEvent 被發布到 ApplicationContext 上,那個 bean 會被通知。

Spring 提供了以下的標準事件:

序號Spring 內置事件 & 描述
1

ContextRefreshedEvent

ApplicationContext 被初始化或刷新時,該事件被發布。這也可以在 ConfigurableApplicationContext 接口中使用 refresh() 方法來發生。

2

ContextStartedEvent

當使用 ConfigurableApplicationContext 接口中的 start() 方法啟動 ApplicationContext 時,該事件被發布。你可以調查你的數據庫,或者你可以在接受到這個事件后重啟任何停止的應用程序。

3

ContextStoppedEvent

當使用 ConfigurableApplicationContext 接口中的 stop() 方法停止 ApplicationContext 時,發布這個事件。你可以在接受到這個事件后做必要的清理的工作。

4

ContextClosedEvent

當使用 ConfigurableApplicationContext 接口中的 close() 方法關閉 ApplicationContext 時,該事件被發布。一個已關閉的上下文到達生命周期末端;它不能被刷新或重啟。

5

RequestHandledEvent

這是一個 web-specific 事件,告訴所有 bean HTTP 請求已經被服務。

由于 Spring 的事件處理是單線程的,所以如果一個事件被發布,直至并且除非所有的接收者得到的該消息,該進程被阻塞并且流程將不會繼續。因此,如果事件處理被使用,在設計應用程序時應注意。

監聽上下文事件

為了監聽上下文事件,一個 bean 應該實現只有一個方法 onApplicationEvent() 的 ApplicationListener 接口。因此,我們寫一個例子來看看事件是如何傳播的,以及如何可以用代碼來執行基于某些事件所需的任務。

讓我們在恰當的位置使用 Eclipse IDE,然后按照下面的步驟來創建一個 Spring 應用程序:

步驟描述
1創建一個名稱為 SpringExample 的項目,并且在創建項目的 src 文件夾中創建一個包 com.tutorialspoint。
2使用 Add External JARs 選項,添加所需的 Spring 庫,解釋見 Spring Hello World Example 章節。
3在 com.tutorialspoint 包中創建 Java 類 HelloWorld、CStartEventHandler、CStopEventHandler 和 MainApp。
4src 文件夾中創建 Bean 的配置文件 Beans.xml。
5最后一步是創建的所有 Java 文件和 Bean 配置文件的內容,并運行應用程序,解釋如下所示。

這里是 HelloWorld.java 文件的內容:

package com.tutorialspoint;
public class HelloWorld {
   private String message;
   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

下面是 CStartEventHandler.java 文件的內容:

package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class CStartEventHandler 
   implements ApplicationListener<ContextStartedEvent>{
   public void onApplicationEvent(ContextStartedEvent event) {
      System.out.println("ContextStartedEvent Received");
   }
}

下面是 CStopEventHandler.java 文件的內容:

package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;
public class CStopEventHandler 
   implements ApplicationListener<ContextStoppedEvent>{
   public void onApplicationEvent(ContextStoppedEvent event) {
      System.out.println("ContextStoppedEvent Received");
   }
}

下面是 MainApp.java 文件的內容:

package com.tutorialspoint;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ConfigurableApplicationContext context = 
      new ClassPathXmlApplicationContext("Beans.xml");

      // Let us raise a start event.
      context.start();

      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

      obj.getMessage();

      // Let us raise a stop event.
      context.stop();
   }
}

下面是配置文件 Beans.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
      <property name="message" value="Hello World!"/>
   </bean>

   <bean id="cStartEventHandler" 
         class="com.tutorialspoint.CStartEventHandler"/>

   <bean id="cStopEventHandler" 
         class="com.tutorialspoint.CStopEventHandler"/>

</beans>

一旦你完成了創建源和 bean 的配置文件,我們就可以運行該應用程序。如果你的應用程序一切都正常,將輸出以下消息:

ContextStartedEvent Received
Your Message : Hello World!
ContextStoppedEvent Received
關閉
程序員人生
主站蜘蛛池模板: 黄色的网站在线观看 | 久久69国产一区二区蜜臀 | 亚洲欧洲日产v特级毛片 | 亚洲综合色一区 | 日韩欧美在线观看视频 | 欧美日韩亚洲国产 | 欧美精品中文字幕久久二区 | 亚洲精品视频一区二区三区 | 午夜一区 | 国产精品久久久久久久久久久久久 | 成年网站在线 | 国产剧情在线 | 免费日韩在线 | 亚洲免费电影一区 | 亚洲精彩视频在线观看 | 999久久久精品视频 国产第91页 | 欧美日韩国产精品成人 | 欧美三区 | 精品一区久久 | 精品久久久久久久久久久下田 | 国产精品热| 欧美.com | 国产suv一区| 国产传媒一区二区三区 | 国产精品爽爽久久久久久 | 亚洲精品久久久久久一区二区 | 欧美在线免费观看 | 黄网在线免费观看 | 色综合久久88色综合天天 | 三级日韩 | 日韩一区二区精品 | aa一级视频 | 只有这里有精品 | 成人福利视频网站 | 国产电影一区 | 久久精品黄色 | 亚洲四区 | 精品少妇一区二区 | 久久久久久精彩视频 | 欧美一区二区三区免费看 | 天天干婷婷 |