zoukankan      html  css  js  c++  java
  • SpringBoot: 4.SpringBoot整合listener(转)

     

    整合方式一:通过注解扫描完成 Listener 组件的注册

    1、编写listener

    复制代码
    package com.bjsxt.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    /**
     * Created by Administrator on 2019/2/5.
     */
    @WebListener
    public class FirstListener implements ServletContextListener{
    
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            System.out.println("FirstListener初始化......");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
    
        }
    }
    复制代码

    2、编写启动类

    复制代码
    package com.bjsxt;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    /**
     * Created by Administrator on 2019/2/4.
     */
    @SpringBootApplication
    @ServletComponentScan   //在springboot启动时,会扫描@WebServlet、@WebFilter、@WebListener等,并将该类实例化
    public class App {
    
        public static void main(String[] args){
            SpringApplication.run(App.class,args);
        }
    }
    复制代码

     

    整合方式二:通过方法完成 Listener 组件注册

    1、编写listener

    复制代码
    package com.bjsxt.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    /**
     * Created by Administrator on 2019/2/5.
     */
    public class SecondListener implements ServletContextListener{
    
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            System.out.println("SecondListener初始化......");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
    
        }
    }
    复制代码

    2、编写启动类

    复制代码
    package com.bjsxt;
    
    import com.bjsxt.filter.SecondFilter;
    import com.bjsxt.listener.SecondListener;
    import com.bjsxt.servlet.SecondServlet;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    
    /**
     * Created by Administrator on 2019/2/4.
     */
    @SpringBootApplication
    public class App2 {
    
        public static void main(String[] args){
            SpringApplication.run(App2.class,args);
        }
    
        /**
         * 注册Servlet
         * @return
         */
        @Bean
        public ServletRegistrationBean getServletRegistrationBean(){
            ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
            bean.addUrlMappings("/second");
            return bean;
        }
    
        /**
         * 注册Filter
         * @return
         */
        @Bean
        public FilterRegistrationBean getFilterRegistrationBean(){
            FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
            bean.addUrlPatterns(new String[]{"/second"});
            return bean;
        }
    
    
        /**
         * 注册Listener
         * @return
         */
        @Bean
        public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
            ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
            return bean;
        }
    }
    复制代码

    运行启动类,监听器初始化输出的信息就会在控制台输出

     
     
  • 相关阅读:
    大数加法、乘法实现的简单版本
    hdu 4027 Can you answer these queries?
    zoj 1610 Count the Colors
    2018 徐州赛区网赛 G. Trace
    1495 中国好区间 尺取法
    LA 3938 动态最大连续区间 线段树
    51nod 1275 连续子段的差异
    caioj 1172 poj 2823 单调队列过渡题
    数据结构和算法题
    一个通用分页类
  • 原文地址:https://www.cnblogs.com/kuangzhisen/p/10427134.html
Copyright © 2011-2022 走看看