IoC容器(5):アプリケーションEventPublisher
10. ApplicationEventPublisher
イベントプログラミングに必要なインタフェースを提供します.ファイバモードインプリメンテーション.
ApplicationContext extends ApplicationEventPublisher
public class MyEvent extends ApplicationEvent {
private int data;
public MyEvent(Object source) {
super(source);
}
public MyEvent(Object source, int data) {
this.source = source;
this.data = data;
}
public int getData(){
return data;
}
}
ApplicationContextには、このイベントをトリガーする機能があります.イベントを起動する方法
@Component
public class AppRunner implements ApplicationRunner {
/*
@Autowired
ApplicationContext applicationContext;
*/
@Autowired
ApplicationEventPublisher publishEvent;
@Override
public void run(ApplicationArguments args) throws Exception {
publishEvent.publishEvent(new MyEvent(this, 100));
}
}
このイベントを処理するにはイベントハンドラが必要ですが、イベントハンドラは空に登録する必要があります.イベントの処理方法
@Component
public class MyEventHandler implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent myEvent) {
System.out.println("이벤트 받았다. 데이터는 " + myEvent.getData());
}
}
public class MyEvent {
private int data;
private Object source;
public MyEvent(Object source, int data) {
this.source = source;
this.data = data;
}
public Object getSource(){
return source;
}
public int getData(){
return data;
}
}
@Component
public class MyEventHandler {
@EventListener
public void handle(MyEvent event){
System.out.println("이벤트 받았다. 데이터는 " + event.getData());
}
EventHandlerにはいろいろな種類があると仮定します@Component
public class MyEventHandler {
@EventListener
public void handle(MyEvent event){
System.out.println(Thread.currentThread().toString());
System.out.println("이벤트 받았다. 데이터는 " + event.getData());
}
@Component
public class AnotherHandler {
@EventListener
public void handler(MyEvent myEvent){
System.out.println(Thread.currentThread().toString());
System.out.println("Another " + myEvent.getData());
}
}
では、この2つのハンドルが実行されます.デフォルトでは順番に実行されます(synchronized).これを確認するには、スレッドの名前を印刷するだけで、両方ともメインスレッドで実行します.@Component
public class AnotherHandler {
@EventListener
@Order(Ordered.HIGHEST_PRECEDENCE + 2)
public void handler(MyEvent myEvent){
System.out.println(Thread.currentThread().toString());
System.out.println("Another " + myEvent.getData());
}
}
@Component
public class MyEventHandler {
@EventListener
@Order(Ordered.HIGHEST_PRECEDENCE)
public void handle(MyEvent event){
System.out.println(Thread.currentThread().toString());
System.out.println("이벤트 받았다. 데이터는 " + event.getData());
}
@SpringBootApplication
@EnableAsync
public class Springtest6Application {
public static void main(String[] args) {
SpringApplication.run(Springtest6Application.class, args);
}
}
@Component
public class AnotherHandler {
@EventListener
@Async
public void handler(MyEvent myEvent){
System.out.println(Thread.currentThread().toString());
System.out.println("Another " + myEvent.getData());
}
}
@Component
public class MyEventHandler {
@EventListener
@@Async
public void handle(MyEvent event){
System.out.println(Thread.currentThread().toString());
System.out.println("이벤트 받았다. 데이터는 " + event.getData());
}
スプリングが提供する基本イベント(ApplicationContext)に関連@Component
public class MyEventHandler {
@EventListener
@Async
public void handle(MyEvent event){
System.out.println(Thread.currentThread().toString());
System.out.println("이벤트 받았다. 데이터는 " + event.getData());
}
@EventListener
@Async
public void handle(ContextRefreshedEvent event){
System.out.println(Thread.currentThread().toString());
System.out.println("ContextRefreshedEvent");
//var applicationContext = event.getApplicationContext()
}
@EventListener
@Async
public void handle(ContextClosedEvent event){
System.out.println(Thread.currentThread().toString());
System.out.println("ContextClosedEvent");
//var applicationContext = event.getApplicationContext()
}
}
リファレンスReference
この問題について(IoC容器(5):アプリケーションEventPublisher), 我々は、より多くの情報をここで見つけました https://velog.io/@jsj3282/IoC-컨테이너-5テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol