springboot 통합 Listener 의 두 가지 방식 을 자세히 알 아 보 세 요.

1.주 해 를 통 해

시작 클래스 작성

package cn.bl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class App {
 public static void main(String[] args) {
 SpringApplication.run(App.class, args);
 }
}
 모니터 를 작성 하 다

package cn.bl.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class FirstListener implements ServletContextListener{
 @Override
 public void contextInitialized(ServletContextEvent sce) {
 System.out.println("init .. ");
 }
 @Override
 public void contextDestroyed(ServletContextEvent sce) {
 System.out.println("desroyed .. ");
 }
}
 앱 을 실행 할 때

2.함수 통과

package cn.bl.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SecondListener implements ServletContextListener{
 @Override
 public void contextInitialized(ServletContextEvent sce) {
 System.out.println("second servletListener init .. ");
 }
 @Override
 public void contextDestroyed(ServletContextEvent sce) {
 System.out.println("second servletListener destroy .. ");
 }
}

package cn.bl;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import cn.bl.listener.SecondListener;
@SpringBootApplication
public class App2 {
 public static void main(String[] args) {
 SpringApplication.run(App2.class, args);
 }
 @Bean
 public ServletListenerRegistrationBean<SecondListener>getBean(){
 ServletListenerRegistrationBean<SecondListener>bean = new ServletListenerRegistrationBean<>(new SecondListener());
 return bean;
 }
}
총결산
위 에서 말씀 드 린 것 은 편집장 님 께 서 소개 해 주신 springboot 통합 Listener 의 두 가지 방식 입 니 다.여러분 께 도움 이 되 셨 으 면 좋 겠 습 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.편집장 님 께 서 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기