Spring Boot learning (7) springboot integration of the three components (Servlet, Filter, Listener)

EDITORIAL : Recently at night time watching video online learning Spring Boot, this blog is a little bit of learning and summary records,Technology is open source, knowledge is shared.
If you are interested in Spring Boot, I can focus on dynamic, we learn together.With the knowledge to change the fate, let the family live a better life

First, configure the Embedded Servlet container

Action : to modify the configuration Servlet Container

Note: Spring Boot 2.x version is quite different before the former EmbeddedServletContainerCustomizer, but in the new version using WebServerFactoryCustomizer

	/**
     * 配置嵌入式的Servlet容器
     *
     * @return
     */
    @Bean //一定要将这个定制器加入到容器中
    public WebServerFactoryCustomizer webServerFactory() {
        return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {
            //定制嵌入式的Servlet容器相关的规则
            @Override
            public void customize(ConfigurableServletWebServerFactory factory) {
                factory.setPort(8088);
            }
        };
    }

Second, the integration of the three components (Servlet, Filter, Listener)

Because the default is to use the jar package SpringBoot way to start the embedded Servle t container to start web application SpringBoot no web.xml file. Therefore, to register the three components in the following manner.

1, Registration Servlet

MySevlet

public class MySevlet extends HttpServlet {

    /**
     * 处理get请求
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @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");
    }
}

Server-related configuration entirely on MyServerConfig

@Configuration
public class MyServerConfig {

    /**
     * 注册servlet组件
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean myServlet() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MySevlet(), "/myServlet");
        // 设置启动顺序
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }
}

Here Insert Picture Description

2, registration Filter

MyFilter

public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("MyFilter 执行了。。。");
        // 放行请求
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}
/**
     * 注册filter组件
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean myFilter() {
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        // 设置filter
        filterRegistrationBean.setFilter(new MyFilter());
        // 设置拦截uri的路径
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello", "/myServlet"));
        return filterRegistrationBean;
    }

Here Insert Picture Description
Here Insert Picture Description

3, registration Listener

MyListener

public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("当前web应用启动。。。");
    }

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

 /**
     * 注册Listener
     *
     * @return
     */
    @Bean
    public ServletListenerRegistrationBean myListener() {
        ServletListenerRegistrationBean<MyListener> registrationBean =
                new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }

Here Insert Picture Description

Published 73 original articles · won praise 795 · views 50000 +

Guess you like

Origin blog.csdn.net/weixin_43570367/article/details/103688875