码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot学习4:springboot整合listener

时间:2019-02-05 11:46:46      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:ati   min   spring   gets   listen   frame   add   att   print   

整合方式一:通过注解扫描完成 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;
    }
}

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

SpringBoot学习4:springboot整合listener

标签:ati   min   spring   gets   listen   frame   add   att   print   

原文地址:https://www.cnblogs.com/duanrantao/p/10352550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!