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);
}
}
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) {

}
}
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() {

}
}
上面配置完了,需要配置一个核心的注解@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 {};

}

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

@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,下面提供的三个配置和方案一采用的方式能够达到统一的效果

@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
---------------------
作者:夜宿山寺
来源:CSDN
原文:https://blog.csdn.net/king_is_everyone/article/details/53116744
版权声明:本文为博主原创文章,转载请附上博文链接!

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的 因为有可能打包之后是一个ja ...

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

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

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

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

  5. ServletContextInitializer添加 servlet filter listener

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

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

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

  7. SpringBoot系列教程之Bean之指定初始化顺序的若干姿势

    上一篇博文介绍了@Order注解的常见错误理解,它并不能指定 bean 的加载顺序,那么问题来了,如果我需要指定 bean 的加载顺序,那应该怎么办呢? 本文将介绍几种可行的方式来控制 bean 之间 ...

  8. servlet filter listener interceptor 知识点

    这篇文章主要介绍 servlet filter listener interceptor 之 知识点.博文主要从 概念,生命周期,使命介绍其区别.详情如下:   概念 生命周期 使命 servlet ...

  9. JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下)

    JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下) Reference

随机推荐

  1. Spring第四篇

    在spring第三篇中介绍了bean元素属性 在第四篇中介绍spring注入的方式 1 set方法注入 建立一个User类 创建私有的属性 set  get 方法  重写toString方法 代码如下 ...

  2. 数组队列C++实现

    template <typename _T>class CArrayQueue {public:    CArrayQueue() {        m_rear = 0;        ...

  3. 文本PDG转PDF

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2008.08.03更新:2008.08.24 补充说明:此文成文较早,当时文本PDG如果不想用官方浏览器SSREADER看, ...

  4. IIS如何避免子web应用程序中继承根目录web.config配置

    1.一种方式,需要改动根目录的web.config(不是很推荐) <?xml version="1.0"?> <configuration> <loc ...

  5. C# System.Timers.Timer定时器的使用和定时自动清理内存应用

    项目比较大有时候会比较卡,虽然有GC自动清理机制,但是还是有不尽人意的地方.所以尝试在项目启动文件中,手动写了一个定时器,定时清理内存,加快项目运行速度. public class Program { ...

  6. MasterPage + UpdatePanel + FileUpload

    上传文件在母版页与Ajax的UpdatePanel的环境进行.由于在母版内使用Ajax,建议使用AjaxControlToolkit.dll组件,去微软官网下载后,并拉入BIN目录中. 然后去web. ...

  7. Kafka 练习题

    一.选择题 Kafka服务器默认能接收的最大消息是多大? (单选) A A:1M B:10M C:100M D:没有大小限制,因为支持大数据 2.Kafka的特性(多选)  ABCD A:高吞吐量.低 ...

  8. java插入图片到数据库(可以批量)

    package sundun.zfpt.gg.web; import java.io.File; import java.io.FileInputStream; import java.sql.Con ...

  9. 获取WPF窗体/控件的句柄/当前进程的句柄

    1.在WPF中,获取当前窗体的句柄与WINFORM中不一样: WINFORM直接获取:this.Handle----------this是窗体的类名,handle就是句柄. 2.WPF中先引用命名空间 ...

  10. 「产品经理全连接系列2」企业如何开展敏捷或DevOps的研发变革

    大家好,我是华为云的产品经理 恒少: 作为布道师和产品经理,出差各地接触客户是常态,经常和华为云的客户交流.布道.技术沙龙,但是线下交流,覆盖的用户总还是少数. 我希望可以借线上的平台,和用户持续交流 ...