1.介绍

通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的

因为有可能打包之后是一个jar包的形式,这种情况下如何解决?SpringBoot 提供了两种方案进行解决

2.快速开始

2.1 方案一

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

IndexServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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);
    }
  }

IndexListener

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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) {
 
   }
 }

IndexFilter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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() {
 
    }
  }

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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
@SpringBootApplication
@ServletComponentScan
public class AppApplication {
 
  public static void main(String[] args) throws Exception {
    SpringApplication.run(AppApplication.class, args);
  }
 
}

2.2 方案二

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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;
}

3.总结

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

4.扩展

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

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

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

SpringBoot初始教程之Servlet、Filter、Listener配置详解的更多相关文章

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

    1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个ja ...

  2. SpringBoot初始教程之Servlet、Filter、Listener配置

    1.介绍通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个jar ...

  3. SpringBoot—整合log4j2入门和log4j2.xml配置详解

    关注微信公众号:CodingTechWork,一起学习进步. 引言   对于一个线上程序或者服务而言,重要的是要有日志输出,这样才能方便运维.而日志的输出需要有一定的规划,如日志命名.日志大小,日志分 ...

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

    1.介绍 有关Session的管理方式这里就不再进行讨论,目前无非就是三种单机Session(基于单机内存,无法部署多台机器).基于Cookie(安全性差).基于全局的统一Session管理(redi ...

  5. servlet的web-xml配置详解

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  6. servlet的xml配置详解

    <?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns ...

  7. SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式

    在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...

  8. ServletContextInitializer添加 servlet filter listener

    ServletContextInitializer添加 servlet filter listener https://www.cnblogs.com/pomer-huang/p/9639322.ht ...

  9. SpringBoot系列教程之Bean加载顺序之错误使用姿势辟谣

    在网上查询 Bean 的加载顺序时,看到了大量的文章中使用@Order注解的方式来控制 bean 的加载顺序,不知道写这些的博文的同学自己有没有实际的验证过,本文希望通过指出这些错误的使用姿势,让观文 ...

随机推荐

  1. 算法Sedgewick第四版-第1章基础-018一解决不能声明泛型数组的两咱方法(强转或反射)

    1. /****************************************************************************** * Compilation: ja ...

  2. ZROI2018普转提day1t1

    传送门 分析 我们先二分一下最终的平均值mid,然后让序列中的每一个数都减去这个mid,之后用新序列的前缀和建一棵线段树,枚举起点i,然后求出此时在i+L-1~i+R-1范围内的前缀和的最大值,用这个 ...

  3. 带有通配符的字符串匹配算法-C/C++

    日前某君给我出了这样一道题目:两个字符串,一个是普通字符串,另一个含有*和?通配符,*代表零个到多个任意字符,?代表一个任意字符,通配符可能多次出现.写一个算法,比较两个字符串是否相等. 我花了四个小 ...

  4. python 常用的一些库

    AllPairs 2.0.1Appium-Python-Client 0.24asn1crypto 0.24.0attrs 17.4.0AutoItLibrary 1.1bcrypt 3.1.4bea ...

  5. java 英文笔记

    JDBC涉及到的单词 Driver 驱动器 DriverManager (Manager 处理者,经理,管理人; 干事,理事; 〈美〉(政党等的)领袖; 策士,干才,干练的人;) create Sta ...

  6. Vue执行方法,方法获取data值,设置data值,方法传值

    方法写在methods中 v-on:click="run()" @click="run()" 方法获取data中的数据通过this.数据获取 方法设置data中 ...

  7. Unity3D 接口使用

    C#怎么实现多继承? 说起多继承,首先大家可以想想这个问题:你知道在C#中怎么实现多继承吗? 主流的答案无非2种. 答案一:用接口啊,一个类可以继承自多个接口的.答案二:C#不支持多继承,C++才支持 ...

  8. ubuntu - 14.04,创建菜单

    我们有的时候可能会把一个执行程序放到一个位置,随后我们希望在ubuntu的菜单里面加入它,这个操作非常简单: 我在Gnome桌面里,选择:“系统工具”->“首选项”->"主菜单& ...

  9. SingleClass单例类

    前言 对于一个单例类,无论初始化单例对象多少次,在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死,该实例对象就不会被释放,并且该对象是全局的,能够被整个系统访问到. 在应用这个模 ...

  10. soj 131 找题

    soj 131 找题 给出两个长度为n,都含k个1的字符串A,B.现在令\(a_1,a_2,\dots,a_k\)是A中1的下标,\(b_1,b_2,\dots,b_k\)是B中1的下表,然后将a,b ...