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

最新文章

  1. json数据格式及json校验格式化工具简单实现
  2. ASP.NET MVC之分部视图和ChildAction(三)
  3. 关于C#基础
  4. sql server存储过程编程
  5. 如何解决winows启动后出现grub?
  6. ubuntu下设置Android手机驱动
  7. 字符串p型编码
  8. HDU 1532 Drainage Ditches EK算法 flod算法
  9. PowerDesigner提示This data item is already used in a primary identifier.的处理
  10. destoon程序中qq号码,手机号,msn必选项实现方法
  11. opencv-形态处理
  12. MySQL 表的命令
  13. com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method findUserByUserNo in the service wusc.edu.facade.user.service.PmsUserFacade.
  14. IOS 中openGL使用教程4(openGL ES 入门篇 | 离屏渲染)
  15. Java虚拟机运行时内存区域简析
  16. Linux内存管理 (25)内存sysfs节点解读
  17. python之路day05--字典的增删改查,嵌套
  18. 【NOI2019模拟】搬砖
  19. go语言的排序和去重
  20. CSS 3D transforms

热门文章

  1. Javascript基础系列之(五)条件语句(逻辑操作符)
  2. 前端(五):JavaScript面向对象之内建对象
  3. CentOS 7.2 安装 MySQL 5.6.24
  4. About custom Theme and Style
  5. websocket 和 dwr 做web端即时通信
  6. mysql 去除重复数据
  7. CSS性能优化新属性:will-change
  8. 3 TFRecord样例程序实战
  9. mac phpstrom 环境配置
  10. Azure 中 Linux 虚拟机的大小