イベントがSpringによって提供されたApplicationContextに投稿され、リスナーによって検出されると、対応する処理方法が実行されます。
イベント自体<BR />イベントは、Springが提供するApplicationEventを継承する必要があるカスタムクラスです。
@DataPublic Class MyEvent拡張ApplicationEvent {private string msg; public MyEvent(オブジェクトソース、文字列msg){super(source); this.msg = msg; }}イベントリスニング
基本的な方法は、 ApplicationListenerインターフェイスを実装し、リスナーをカスタマイズし、 onApplicationEvent()メソッドを実装してから、 ApplicationContextに追加することです。
例えば:
Public Class MyListenerはApplicationListener <MyEvent> {@Override public void onapplicationEvent(myEvent event){system.out.print( "Speaking MyEvent Event"); }} ... //リスナーをスプリングブートパブリックstatic void main(string [] args)のスタートアップクラスに追加する{springApplication application = new SpringApplication(myApplication.class); Application.AddListeners(new MyListener()); application.run(args); } Annotation @ EventListener (推奨)を使用することもできます。原則は、このアノテーションをスキャンし、リスナーを作成し、 ApplicationContextに追加することもできます。
@component @slf4jpublic class myeventhandler {@eventlistener public void handerevent(myevent event){ log.info("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------イベントリリース
Context Object Publishing Method ConfigurableApplicationContext::publishEvent()を使用して公開できます。
ApplicationEventPublisherAwareインターフェイスを実装して公開することもできます(推奨)。
@component@slf4jpublic class eventservice applicationeventpublisheraware {public applicationeventpublisher publisher; @Override public void setApplicationEventPublisher(ApplicationEventPublisher ApplicationEventPublisher){this.publisher = applicationEventPublisher; } public string doeventwork(string msg){ log.info("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- msg); publish.publishevent(event);テストコード
@springboottest @runwith(springrunner.class)パブリッククラスのeventservicetest {@autowired private eventserviceサービス; @test public void eventtest(){string msg = "java code"; service.doeventwork(msg); }}知らせ
2つのイベント間に相続関係がある場合、サブクラスイベントが最初に聴き、次に親クラスが処理後に聴きます。
// myevent2 extends myevent@component@slf4jpublic class myeventhandler {@eventlistener public void handlevent(myevent event){ log.info("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- log.info( "----------------------- {}、event.getmsg();サブクラスイベントMyEvent2を公開すると、ログは次のとおりです。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。