Spring-event-handling-in-spring

提供:Dev Guides
移動先:案内検索

春のイベント処理

すべての章で、SpringのコアはBeanの完全なライフサイクルを管理する ApplicationContext であることを確認しました。 ApplicationContextは、Beanをロードするときに特定のタイプのイベントを公開します。 たとえば、コンテキストの開始時に_ContextStartedEvent_が発行され、コンテキストの停止時に_ContextStoppedEvent_が発行されます。

_ApplicationContext_でのイベント処理は、_ApplicationEvent_クラスと_ApplicationListener_インターフェイスを介して提供されます。 したがって、豆を実装_ApplicationListener_場合、_ApplicationEvent_はApplicationContextのに公開されるたびに、そのBeanが通知されます。

春は、次の標準イベントを提供します-

Sr.No. Spring Built-in Events & Description
1

ContextRefreshedEvent

このイベントは、_ApplicationContext_が初期化または更新されると公開されます。 これは、_ConfigurableApplicationContext_インターフェースのrefresh()メソッドを使用して発生させることもできます。

2

ContextStartedEvent

このイベントは、_ConfigurableApplicationContext_インターフェースのstart()メソッドを使用して_ApplicationContext_が開始されると公開されます。 データベースをポーリングするか、このイベントを受け取った後に停止したアプリケーションを再起動できます。

3

ContextStoppedEvent

このイベントは、_ConfigurableApplicationContext_インターフェースのstop()メソッドを使用して_ApplicationContext_が停止されると公開されます。 このイベントを受け取った後、必要なハウスキープ作業を行うことができます。

4

ContextClosedEvent

このイベントは、_ConfigurableApplicationContext_インターフェースのclose()メソッドを使用して_ApplicationContext_が閉じられると公開されます。 閉じたコンテキストは、その寿命の終わりに達します。更新または再起動できません。

5

RequestHandledEvent

これは、HTTPリクエストが処理されたことをすべてのBeanに伝えるWeb固有のイベントです。

Springのイベント処理はシングルスレッドであるため、イベントが公開された場合、すべての受信者がメッセージを受信するまで、プロセスがブロックされ、フローは続行されません。 したがって、イベント処理を使用する場合は、アプリケーションの設計時に注意が必要です。

コンテキストイベントを聞く

コンテキストイベントをリッスンするには、Beanは* onApplicationEvent()*メソッドを1つだけ持つ_ApplicationListener_インターフェイスを実装する必要があります。 したがって、イベントがどのように伝播し、特定のイベントに基づいて必要なタスクを実行するようにコードを配置する方法を確認するための例を作成しましょう。

動作するEclipse IDEを用意し、次の手順を実行してSpringアプリケーションを作成します。

Step Description
1 Create a project with a name SpringExample and create a package com.finddevguides under the *src *folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld, CStartEventHandler, CStopEventHandler and MainApp under the com.finddevguides package.
4 Create Beans configuration file Beans.xml under the* src* folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.
*HelloWorld.java* ファイルの内容は次のとおりです。
package com.finddevguides;

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.finddevguides;

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.finddevguides;

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.finddevguides;

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.finddevguides.HelloWorld">
      <property name = "message" value = "Hello World!"/>
   </bean>

   <bean id = "cStartEventHandler" class = "com.finddevguides.CStartEventHandler"/>
   <bean id = "cStopEventHandler" class = "com.finddevguides.CStopEventHandler"/>

</beans>

ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行しましょう。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-

ContextStartedEvent Received
Your Message : Hello World!
ContextStoppedEvent Received

必要に応じて、独自のカスタムイベントを公開し、後で同じイベントをキャプチャして、それらのカスタムイベントに対してアクションを実行できます。 独自のカスタムイベントの作成に興味がある場合は、link:/spring/custom_events_in_spring [Springのカスタムイベント]を確認してください。