SpringBoot-整合Listener

版权声明:XiangYida https://blog.csdn.net/qq_36781505/article/details/83855849

SpringBoot-整合Listener

与整合前面的是一样的,直接看代码吧

  • 方式一、注解扫描
    监听器
@WebListener
public class FirstListener implements ServletContextListener {
  @Override
  public void contextDestroyed(ServletContextEvent arg0) {
    // TODO Auto-generated method stub
  }
  @Override
  public void contextInitialized(ServletContextEvent arg0) {
    System.out.println("Listener...init......");
  }
}

启动类

@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
  SpringApplication.run(App.class, args);
  }
}
  • 方式二、方法完成组件注册
public class SecondListener implements ServletContextListener {
  @Override
  public void contextDestroyed(ServletContextEvent arg0) {
      // TODO Auto-generated method stub
    }
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
      System.out.println("SecondListener..init.....");
    }
}
@SpringBootApplication
public class App2 {
    public static void main(String[] args) {
      SpringApplication.run(App2.class, args);
    }
    @Bean
    public ServletListenerRegistrationBean<SecondListener>getServletListenerRegistrationBean(){
      ServletListenerRegistrationBean<SecondListener> bean= new
        ServletListenerRegistrationBean<SecondListener>(new SecondListener());
        return bean;
      }
}

猜你喜欢

转载自blog.csdn.net/qq_36781505/article/details/83855849