SpringBoot整合SpringMVC之Servlet,Filter,Listener

SpringBoot整合SpringMVC之Servlet,Filter,Listener

LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code,Keep progress,make a better result.
Survive during the day and develop at night。

目录

概 述

应用场景:在使用的第三方类库,更多的提供的是java.servlet中的组件,不用与SpringMVC 耦合
eg:eg:shiro权限认证:需要配置shiro提供的ShiroFilterFactoryBean
总的实现过程:

配置:

bean的方式: 在mvc的配置类中,添加三大组件的配置,
ServletRegistrationBean --> Servlet
FilterRegistrationBean --> Filter
ServletListenerRegistrationBean -->Listener

注册的方式

Servlet -->extend HttpServlet @WebServlet(urlPatterns = “/test/02”)
Filter -->implements Filter @WebFilter("/test/*")

Listener --> implements ServletContextListener @WenListener

相关工具如下:


```java
 @Bean
    public ServletRegistrationBean testServlet01() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean<>(new HttpServlet() {
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                logger.info("[doGet][uri: {}]",req.getRequestURI());
            }
        });
        servletRegistrationBean.setUrlMappings(Collections.singleton("/test/01"));
        return servletRegistrationBean;
    }
 
    @Bean
    public FilterRegistrationBean testFilter01() {
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>(new Filter() {
            @Override
            public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                logger.info("[doFilter]");
                filterChain.doFilter(servletRequest,servletResponse);
            }
        });
        filterFilterRegistrationBean.setUrlPatterns(Collections.singleton("/test/*"));
        return filterFilterRegistrationBean;
    }
 
    @Bean
    public ServletListenerRegistrationBean testListener01() {
        return new ServletListenerRegistrationBean<>(new ServletContextListener() {
            @Override
            public void contextInitialized(ServletContextEvent sce) {
                logger.info("[contextInitialized]");
            }
 
            @Override
            public void contextDestroyed(ServletContextEvent sce) {
 
            }
        });
    }
### 注解:

```java
@WebServlet(urlPatterns = "/test/02")
public class TestServlet02 extends HttpServlet {
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.info("[doGet][uri: {}]", req.getRequestURI());
    }
}

Testiter 02 的方法


@WebFilter("/test/*")
public class TestFilter02 implements Filter {
 
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        logger.info("[doFilter]");
        filterChain.doFilter(servletRequest, servletResponse);
    }

@WebListener
public class TestServletContextListener02 implements ServletContextListener {
 
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("[contextInitialized]");
    }
 
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
 
}


Spring BootCommonreultApplcaition


@SpringBootApplication
@ServletComponentScan
public class SpringbootSpringmvcCommonresultApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringbootSpringmvcCommonresultApplication.class, args);
    }
 


分析:

小结:

参考资料和推荐阅读

1.链接: link.

上一篇:FreeMaker如何使用 QIANKUN 微前端小计


下一篇:MySQL 日期类型及默认设置 (除timestamp类型外,系统不支持其它时间类型字段设置默认值)