Springboot整合Lintener

1,方式一

  【1】通过注解扫描完成Listener组件注册

 

@WebListener
public class FirstListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Lintener...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

 

 

 

  【2】修改启动类

 

@SpringBootApplication
@ServletComponentScan//在Spring Boot启动时会扫描@WebLintener注解,并将该类实例
public class SpringbootwebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebApplication.class, args);
    }

}

 

 

 

2,方式二

  【1】通过方法完成Listener组件注册

 

public class SecondListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("SecondLintener...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

 

 

 

  【2】创建Listener配置类

 

@Configuration
public class ListenerConfig {
    @Bean
    public ServletListenerRegistrationBean getServletListenerRegistrationBean()
    {
        ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new SecondListener());
        return bean;
    };
}

 

posted @ 2020-04-13 13:53  拯代码  阅读(161)  评论(0编辑  收藏  举报