SpringBoot注册Servlet三大组件

1、注册Servlet

ServletRegistrationBean

1.1 编写自己的Servlet类

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author linchong
 * @create 2019-03-25 16:12
 */
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("hello myServlet!");
    }
}

1.2 配置注入Servlet

import com.atguigu.springboot.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author linchong
 * @create 2019-03-25 16:14
 */
@Configuration
public class MyServerConfig {
    //注册三大组件之Servlet
    @Bean
    public ServletRegistrationBean myServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        return servletRegistrationBean;
    }
}

测试:
SpringBoot注册Servlet三大组件_第1张图片

2、注册Filter

FilterRegistrationBean

2.1 编写自定义Filter

package com.atguigu.springboot.filter;

import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author linchong
 * @create 2019-03-25 16:36
 */
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("filter执行。。。");
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

2.2 注册filter

package com.atguigu.springboot.config;

import com.atguigu.springboot.filter.MyFilter;
import com.atguigu.springboot.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author linchong
 * @create 2019-03-25 16:14
 */
@Configuration
public class MyServerConfig {

    //注册三大组件之Filter
    @Bean
    public FilterRegistrationBean myFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return filterRegistrationBean;
    }
}

测试:
SpringBoot注册Servlet三大组件_第2张图片
可以看到filter执行了,并且是在Servlet执行之前拦截请求~

3、注册Listener

ServletListenerRegistrationBean

3.1 自定义Listener

package com.atguigu.springboot.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @author linchong
 * @create 2019-03-25 16:49
 */
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized应用启动....");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextInitialized应用销毁");
    }
}

3.2 配置注入listener

package com.atguigu.springboot.config;

import com.atguigu.springboot.filter.MyFilter;
import com.atguigu.springboot.listener.MyListener;
import com.atguigu.springboot.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author linchong
 * @create 2019-03-25 16:14
 */
@Configuration
public class MyServerConfig {

    //注册三大组件之Listener
    @Bean
    public ServletListenerRegistrationBean myListener() {
        ServletListenerRegistrationBean<MyListener> myListenerServletListenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return myListenerServletListenerRegistrationBean;
    }
}

测试:
SpringBoot注册Servlet三大组件_第3张图片

本文内容来自尚硅谷雷丰阳老师

你可能感兴趣的:(#,Spring,Boot,Servlet三大组件注册,SpringBoot)