转载自

http://blog.csdn.net/king_is_everyone/article/details/53116744

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提供了三种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;
}

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

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

【spring】SpringBoot之Servlet、Filter、Listener配置的更多相关文章

  1. Spring Boot整合Servlet,Filter,Listener,访问静态资源

    目录 Spring Boot整合Servlet(两种方式) 第一种方式(通过注解扫描方式完成Servlet组件的注册): 第二种方式(通过方法完成Servlet组件的注册) Springboot整合F ...

  2. SpringBoot注册Servlet/Filter/Listener

    由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,那么没有web.xml文件,如何配置我们的三大Web基础组件呢? 通过使用XXXRe ...

  3. springboot 注入Servlet,Filter,Listener的方法

    其实就是注入 FilterRegistrationBean . ServletRegistrationBean . ServletListenerRegistrationBean 这三个类   直接上 ...

  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. servlet filter listener interceptor 知识点

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

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

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

  8. SpringBoot---注册Servlet,Filter,Listener

    1.概述 1.1.当使用  内嵌的Servlet容器(Tomcat.Jetty等)时,将Servlet,Filter,Listener  注册到Servlet容器的方法: 1.1.1.直接注册Bean ...

  9. SpringBoot整合WEB开发--(九)整合Servlet,Filter,Listener

    简介: 如果需要整合第三方框架时,可能还是不得不使用Servlet,Filter,Listener,Springboot中也有提供支持. @WebServlet("/my") pu ...

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

    转自:http://www.cnblogs.com/doit8791/p/4209442.html servlet.filter.listener是配置到web.xml中(web.xml 的加载顺序是 ...

随机推荐

  1. 【LA3523 训练指南】圆桌骑士 【双连通分量】

    题意 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少应有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是奇数,以防赞同和反 ...

  2. c# 遍历一个对象里面的全部属性

    比如我现在有一个Student的对象,里面有属性stuName,stuAge,stuGender,我现在该怎么写循环才能遍历这几个属性? Student s=new...... foreach (Sy ...

  3. iOS 10 适配 ATS(app支持https通过App Store审核)

    iOS 10 适配 ATS 一. HTTPS 其实HTTPS从最终的数据解析的角度,与HTTP没有任何的区别,HTTPS就是将HTTP协议数据包放到SSL/TSL层加密后,在TCP/IP层组成IP数据 ...

  4. file_get_contents无法请求https连接的解决方法 php开启curl

    file_get_contents无法请求https连接的解决方法 方法1: PHP.ini默认配置下,用file_get_contents读取https的链接,就会如下错误: Warning: fo ...

  5. [c++] polymorphism without virtual function

    polymorphism without virtual function

  6. 读取位图(bitmap)实现及其要点

    位图的格式如下: 1.文件头信息块 0000-0001 :文件标识,为字母ASCII码“BM”. 0002-0005 :文件大小. 0006-0009 :保留,每字节以“00”填写. 000A-000 ...

  7. arping

    强制交换机刷新MAC arping -I em2(网卡名称) 58.215.88.8(Vip)

  8. date_default_timezone_set()

    date_default_timezone_set("Asia/Shanghai");

  9. 原型工具Axure vs Mockplus ——表格对比 , 你选谁?

    现如今原型设计能力是越来越多的UI/UX.产品经理.提案者和互联网创业者必不可少的技能之一,所以在这里着重向大家介绍这两款非常棒的原型设计工具在表格功能上到底有哪些不同. Axure大家比较熟悉,Mo ...

  10. easyui combogrid 多选加载,保存,显示代码

    1.调用代码 UTIL.SetDict($("#txt_ExcludeIndustry_"), "SECTOR_TYPE", true, true, funct ...