이벤트가 Spring에서 제공 한 ApplicationContext에 게시되고 리스너가 감지하면 해당 처리 방법이 실행됩니다.
이벤트 자체 <br /> 이벤트는 Spring에서 제공 한 ApplicationEvent 상속하는 데 필요한 사용자 정의 클래스입니다.
@datapublic class myevent는 applicationevent {private string msg; public myevent (Object Source, String Msg) {super (source); this.msg = msg; }} 이벤트 듣기
기본 방법은 ApplicationListener 인터페이스를 구현하고, 리스너를 사용자 정의하고, onApplicationEvent() 메소드를 구현 한 다음 ApplicationContext 에 추가하는 것입니다.
예를 들어:
Public Class MyListener는 ApplicationListener <MyEvent> {@Override public void OnApplicationEvent (MyEvent 이벤트) {System.out.print ( "Speaking Myevent Event"); }} ... // SpringBoot의 시작 클래스 공개 정적 무효 메인 (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 handleEvent (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 deeventwork (String msg) { log.info("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- msg); Publisher.publishevent (이벤트); 테스트 코드
@SpringBoottest @Runwith (SpringRunner.class) 공개 클래스 EventServicetest {@autowired 개인 Eventservice Service; @test public void eventTest () {String msg = "Java Code"; Service.DoeventWork (MSG); }} 알아채다
두 이벤트 사이에 상속 관계가있는 경우 서브 클래스 이벤트가 먼저 듣고 처리 후 부모 수업이 들립니다.
// myevent2는 myevent@component@slf4jpublic class myeventhandler {@eventListener public void handleEvent (myevent event) { log.info("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 로그.서브 클래스 이벤트 Myevent2를 게시하면 로그는 다음과 같습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.