Spring-custom-events-in-spring
提供:Dev Guides
春のカスタムイベント
独自のカスタムイベントを作成して公開するには、いくつかの手順を実行する必要があります。 この章の指示に従って、カスタムSpringイベントを作成、公開、および処理します。
Steps | Description |
---|---|
1 | Create a project with a name SpringExample and create a package com.finddevguides under the *src *folder in the created project. All the classes will be created under this package. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create an event class, CustomEvent by extending* ApplicationEvent*. This class must define a default constructor which should inherit constructor from ApplicationEvent class. |
4 | Once your event class is defined, you can publish it from any class, let us say EventClassPublisher which implements ApplicationEventPublisherAware. You will also need to declare this class in XML configuration file as a bean so that the container can identify the bean as an event publisher because it implements the ApplicationEventPublisherAware interface. |
5 | A published event can be handled in a class, let us say EventClassHandler which implements ApplicationListener interface and implements onApplicationEvent method for the custom event. |
6 | Create beans configuration file Beans.xml under the src folder and a MainApp class which will work as Spring application. |
7 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
以下は CustomEventPublisher.java ファイルの内容です
以下は、 CustomEventHandler.java ファイルの内容です。
以下は MainApp.java ファイルの内容です
以下は設定ファイル Beans.xml です
ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行しましょう。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-