SpringBoot初始教程之Servlet、Filter、Listener配置(七)

1.介绍

通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 
因为有可能打包之后是一个jar包的形式,这种情况下如何解决?SpringBoot 提供了两种方案进行解决

2.快速开始

2.1 方案一

方案一采用原生Servlet3.0的注解进行配置、@WebServlet 、@WebListener@WebFilter是Servlet3.0 api中提供的注解 
通过注解可以完全代替web.xml中的配置,下面是一个简单的配置

IndexServlet


    @WebServlet(name = "IndexServlet",urlPatterns = "/hello")
    public class IndexServlet extends HttpServlet {
        @Override
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().print("hello word");
            resp.getWriter().flush();
            resp.getWriter().close();
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req, resp);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

IndexListener

    @WebListener
    public class IndexListener implements ServletContextListener {
        private Log log = LogFactory.getLog(IndexListener.class);

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            log.info("IndexListener contextInitialized");
        }

        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {

        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

IndexFilter


    @WebFilter(urlPatterns = "/*", filterName = "indexFilter")
    public class IndexFilter implements Filter {
        Log log = LogFactory.getLog(IndexFilter.class);

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("init IndexFilter");
        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("doFilter IndexFilter");
            filterChain.doFilter(servletRequest,servletResponse);

        }

        @Override
        public void destroy() {

        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

上面配置完了,需要配置一个核心的注解@ServletComponentScan,具体配置项如下,可以配置扫描的路径


    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Import(ServletComponentScanRegistrar.class)
    public @interface ServletComponentScan {

        @AliasFor("basePackages")
        String[] value() default {};

        @AliasFor("value")
        String[] basePackages() default {};

        Class<?>[] basePackageClasses() default {};

    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

把注解加到入口处启动即可


    @SpringBootApplication
    @ServletComponentScan
    public class AppApplication {

        public static void main(String[] args) throws Exception {
            SpringApplication.run(AppApplication.class, args);
        }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2.2 方案二

方案二是采用自己SpringBoot 配置bean的方式进行配置的,SpringBoot提供了三种BeanFilterRegistrationBeanServletRegistrationBeanServletListenerRegistrationBean 
分别对应配置原生的Filter、Servlet、Listener,下面提供的三个配置和方案一采用的方式能够达到统一的效果


    @Bean
    public ServletRegistrationBean indexServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new IndexServlet());
        registration.addUrlMappings("/hello");
        return registration;
    }

    @Bean
    public FilterRegistrationBean indexFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new IndexFilter());
        registration.addUrlPatterns("/");
        return registration;
    }
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new IndexListener());
        return servletListenerRegistrationBean;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

3.总结

两种方案在使用上有差别,但是在内部SpringBoot的实现上是无差别的,即使使用的是Servlet3.0注解,也是通过扫描注解 
转换成这三种bean的FilterRegistrationBeanServletRegistrationBeanServletListenerRegistrationBean

4.扩展

大家在使用的时候有没有发觉,其实SpringBoot在使用SpringMvc的时候不需要配置DispatcherServlet的,因为已经自动配置了, 
但是如果想要加一些初始配置参数如何解决,方案如下:


    @Bean
    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
        registration.addUrlMappings("*.do");
        registration.addUrlMappings("*.json");
        return registration;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

可以通过注入DispatcherServlet 然后用ServletRegistrationBean包裹一层 动态的加上一些初始参数

本文代码

https://git.oschina.net/wangkang_daydayup/SpringBoot-Learn/tree/master

springboot-7

时间: 2024-08-04 13:36:03

SpringBoot初始教程之Servlet、Filter、Listener配置(七)的相关文章

SpringBoot初始教程之Redis集中式Session管理

1.介绍 有关Session的管理方式这里就不再进行讨论,目前无非就是三种单机Session(基于单机内存,无法部署多台机器).基于Cookie(安全性差).基于全局的统一Session管理(redis.mysql)等多种方式 针对于像淘宝这种超大型网站来说Session如何管理的就无从得知了.但是可以通过yy的方式想象一下,这种大型架构都需要部署多台认证Server,但是一般来说集中式Session无法存储那么多的Session 那么就可以通过UID分片的形式来存储,不同UID分布在不同的Se

servlet/filter/listener/interceptor区别与联系(转)

由于最近两个月工作比较清闲,个人也比较"上进",利用工作空余时间,也继续学习了一下,某天突然想起struts2和struts1的区别的时候,发现为什么struts1要用servlet,而struts2要用filter呢?一时又发现,servlet和filter有什么区别呢?于是看了看web.xml,一时又发现,咦,servlet.filter.listener?还有个interceptor?对于这几个概念,本应是初学者就掌握的东东了,可惜本人基础学的不好,只能是现在补课.于是就有了这篇

servlet &amp; filter &amp; listener &amp; interceptor

1.servlet url传来之后,就对其进行处理,之后返回或转向到某一自己指定的页面.它主要用来在业务处理之前进行控制. 2.filter url传来之后,检查之后,可保持原来的流程继续向下执行. filter可用来进行字符编码的过滤,检测用户是否登陆,禁止页面缓存等 3.servlet 与 filter 都是针对url之类的,而listener是针对对象的操作的.如: session的创建,Spring整合Struts,为Struts的action注入属性,web应用定时任务的实现,在线人数

servlet/filter/listener/interceptor过滤器、监听器、拦截器区分

因为之前一直分不清过滤器和拦截器的区别,所以有了两者差不多的错觉,因此在这里总结下servlet/filter/listener/interceptor过滤器.监听器.拦截器. 在此之前先简单回顾下servlet: 概述:servlet是一种运行服务器端的java应用程序,它工作在客户端请求与服务器响应的中间层. 主要作用:在于交互式地浏览和修改数据,生成动态 Web 内容. 访问流程: 1,客户端发送请求至服务器端: 2,服务器将请求信息发送至 Servlet: 3,Servlet 生成响应内

ArduinoYun教程之OpenWrt-Yun与CLI配置Arduino Yun

ArduinoYun教程之OpenWrt-Yun与CLI配置Arduino Yun OpenWrt-Yun OpenWrt-Yun是基于OpenWrt的一个Linux发行版.有所耳闻的读者应该听说他是一个使用在路由器上的操作系统.其实准确地说OpenWrt是一个嵌入式Linux发型版,它可以安装在各种嵌入式芯片中,如Arduino Yun.在本节中,将为大家介绍OpenWrt-Yun系统相关的知识. 使用SSH连接Arduino Yun SSH是Secure Shell的缩写,它是建立在应用层和

servlet/filter/listener/interceptor区别与联系

由于最近两个月工作比较清闲,个人也比较“上进”,利用工作空余时间,也继续学习了一下,某天突然想起struts2和struts1的区别的时候,发现为什么struts1要用servlet,而struts2要用filter呢?一时又发现,servlet和filter有什么区别呢?于是看了看web.xml,一时又发现,咦,servlet.filter.listener?还有个interceptor?对于这几个概念,本应是初学者就掌握的东东了,可惜本人基础学的不好,只能是现在补课.于是就有了这篇博客. 慢

【转】servlet/filter/listener/interceptor区别与联系

原文:https://www.cnblogs.com/doit8791/p/4209442.html 一.概念: 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性,并且可以动态的生成web页面,它工作在客户端请求与服务器响应的中间层.最早支持 Servlet 技术的是 JavaSoft 的 Java Web Server.此后,一些其它的基于 Java 的 Web Server 开始支持标准的 Servlet API.Servlet 的主要功能在

SpringBoot系列教程之Redis集群环境配置

之前介绍的几篇redis的博文都是基于单机的redis基础上进行演示说明的,然而在实际的生产环境中,使用redis集群的可能性应该是大于单机版的redis的,那么集群的redis如何操作呢?它的配置和单机的有什么区别,又有什么需要注意的呢? 本篇将主要介绍SpringBoot项目整合redis集群,并针对这个过程中出现的问题进行说明,并给出相应的解决方案 I. 环境相关 首先需要安装redis集群环境,可以参考博文:redis-集群搭建手册 然后初始化springboot项目,对应的pom结构如

testng教程之testng.xml的配置和使用,以及参数传递

testng.xml testng.xml是为了更方便的管理和执行测试用例,同时也可以结合其他工具 You can invoke TestNG in several different ways: 你可以用以下三种方式执行测试 With a testng.xml file           直接run as test suite With ant                            使用ant From the command line      从命令行 eclipse