spring boot web相关配置

  • 80436

spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何web相关的配置便能提供web服务,这还得归于spring boot 自动配置的功能(因为加了EnableAutoConfiguration的注解),帮我们创建了一堆默认的配置,以前在web.xml中配置,现在都可以通过spring bean的方式进行配置,由spring来进行生命周期的管理,大多数情况下,我们需要重载这些配置(例如修改服务的启动端口,contextpath,filter,listener,servlet,session超时时间等)

1. servlet配置

当应用只有默认的servlet(即DispatcherServlet)时,映射的url为/,存在其他的servlet时,映射的路径为servlet的注册的beanname(可通过@Component注解修改),创建方式如下:

  1. @Component("myServlet")
  2. public class MyServlet implements Servlet{
  3. /**
  4. *
  5. * @see javax.servlet.Servlet#destroy()
  6. */
  7. @Override
  8. public void destroy() {
  9. System.out.println("destroy...");
  10. }
  11. /**
  12. * @return
  13. * @see javax.servlet.Servlet#getServletConfig()
  14. */
  15. @Override
  16. public ServletConfig getServletConfig() {
  17. return null;
  18. }
  19. /**
  20. * @return
  21. * @see javax.servlet.Servlet#getServletInfo()
  22. */
  23. @Override
  24. public String getServletInfo() {
  25. return null;
  26. }
  27. /**
  28. * @param arg0
  29. * @throws ServletException
  30. * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
  31. */
  32. @Override
  33. public void init(ServletConfig arg0) throws ServletException {
  34. System.out.println("servlet init...");
  35. }
  36. /**
  37. * @param arg0
  38. * @param arg1
  39. * @throws ServletException
  40. * @throws IOException
  41. * @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
  42. */
  43. @Override
  44. public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException,
  45. IOException {
  46. System.out.println("service...");
  47. }

2. filter配置

filter配置的映射是/*,创建方式如下:

  1. @Component("myFilter")
  2. public class MyFilter implements Filter{
  3. /**
  4. *
  5. * @see javax.servlet.Filter#destroy()
  6. */
  7. @Override
  8. public void destroy() {
  9. System.out.println("destroy...");
  10. }
  11. /**
  12. * @param arg0
  13. * @param arg1
  14. * @param arg2
  15. * @throws IOException
  16. * @throws ServletException
  17. * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
  18. */
  19. @Override
  20. public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
  21. throws IOException,
  22. ServletException {
  23. System.out.println("doFilter...");
  24. arg2.doFilter(arg0, arg1);
  25. }
  26. /**
  27. * @param arg0
  28. * @throws ServletException
  29. * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  30. */
  31. @Override
  32. public void init(FilterConfig arg0) throws ServletException {
  33. System.out.println("filter init...");
  34. }
  35. }<span style="font-family: Arial, Helvetica, FreeSans, sans-serif; font-size: 13.3333330154419px; line-height: 17.3333339691162px; background-color: transparent;">   </span>

3. listener配置

  1. @Component("myListener")
  2. public class MyListener implements ServletContextListener{
  3. /**
  4. * @param arg0
  5. * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
  6. */
  7. @Override
  8. public void contextDestroyed(ServletContextEvent arg0) {
  9. System.out.println("contextDestroyed...");
  10. }
  11. /**
  12. * @param arg0
  13. * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
  14. */
  15. @Override
  16. public void contextInitialized(ServletContextEvent arg0) {
  17. System.out.println("listener contextInitialized...");
  18. }
  19. }

如果觉得控制力度不够灵活(例如你想修改filter的映射),spring boot还提供了 ServletRegistrationBean,FilterRegistrationBean,ServletListenerRegistrationBean这3个东西来进行配置

修改filter的映射

  1. @Configuration
  2. public class WebConfig {
  3. @Bean
  4. public FilterRegistrationBean filterRegistrationBean(MyFilter myFilter){
  5. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
  6. filterRegistrationBean.setFilter(myFilter);
  7. filterRegistrationBean.setEnabled(true);
  8. filterRegistrationBean.addUrlPatterns("/bb");
  9. return filterRegistrationBean;
  10. }
  11. }

4. 配置servlet 容器

可以通过两种方式配置servlet容器,一种是通过properties文件,例如:

  1. server.port=8081
  2. server.address=127.0.0.1
  3. server.sessionTimeout=30
  4. server.contextPath=/springboot

完整的配置信息可以看这 http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

另一种方式是java代码的形式:

  1. @Component
  2. public class MyCustomizationBean implements EmbeddedServletContainerCustomizer  {
  3. /**
  4. * @param container
  5. * @see org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer#customize(org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer)
  6. */
  7. @Override
  8. public void customize(ConfigurableEmbeddedServletContainer container) {
  9. container.setContextPath("/sprintboot");
  10. container.setPort(8081);
  11. container.setSessionTimeout(30);
  12. }
  13. }

5. error 处理

spring boot 提供了/error映射来进行错误处理,它会返回一个json来对错误信息进行描述(包含了http状态和异常信息),例如404的错误

当然可以定制错误页面,通过实现ErrorController接口,并将它装配起来即可,如下:

  1. @Controller
  2. public class ErrorHandleController implements ErrorController {
  3. /**
  4. * @return
  5. * @see org.springframework.boot.autoconfigure.web.ErrorController#getErrorPath()
  6. */
  7. @Override
  8. public String getErrorPath() {
  9. return "/screen/error";
  10. }
  11. @RequestMapping
  12. public String errorHandle(){
  13. return getErrorPath();
  14. }
  15. }

还可以这样:

  1. @Component
  2. private class MyCustomizer implements EmbeddedServletContainerCustomizer {
  3. @Override
  4. public void customize(ConfigurableEmbeddedServletContainer container) {
  5. container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/screen/error"));
  6. }
  7. }

6.模板引擎

spring boot 会自动配置 FreeMarker,Thymeleaf,Velocity,只需要在pom中加入相应的依赖即可,例如应用Velocity

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-velocity</artifactId>
  4. </dependency>

默认配置下spring boot会从src/main/resources/templates目录中去找模板

7. jsp限制

如果要在spring boot中使用jsp,得将应用打包成war,这里有配置的example https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp

转-spring boot web相关配置的更多相关文章

  1. spring boot web相关配置

    spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何web相关的配置便能提供web服务,这还得归于spri ...

  2. 【转】spring boot web相关配置

    spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何web相关的配置便能提供web服务,这还得归于spri ...

  3. Spring Boot SSL [https]配置例子

    前言 本文主要介绍Spring Boot HTTPS相关配置,基于自签证书实现: 通过本例子,同样可以了解创建SSL数字证书的过程: 本文概述 Spring boot HTTPS 配置 server. ...

  4. Springboot 系列(六)Spring Boot web 开发之拦截器和三大组件

    1. 拦截器 Springboot 中的 Interceptor 拦截器也就是 mvc 中的拦截器,只是省去了 xml 配置部分.并没有本质的不同,都是通过实现 HandlerInterceptor ...

  5. Spring Boot 2.0 配置图文教程

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...

  6. Springboot 系列(五)Spring Boot web 开发之静态资源和模版引擎

    前言 Spring Boot 天生的适合 web 应用开发,它可以快速的嵌入 Tomcat, Jetty 或 Netty 用于包含一个 HTTP 服务器.且开发十分简单,只需要引入 web 开发所需的 ...

  7. spring boot @ConditionalOnxxx相关注解总结

    Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...

  8. Spring Boot 外部化配置(一)- Environment、ConfigFileApplicationListener

    目录 前言 1.起源 2.外部化配置的资源类型 3.外部化配置的核心 3.1 Environment 3.1.1.ConfigFileApplicationListener 3.1.2.关联 Spri ...

  9. Spring Boot的自动配置

    Spring Boot的自动配置 --摘自https://www.hollischuang.com/archives/1791 随着Ruby.Groovy等动态语言的流行,相比较之下Java的开发显得 ...

随机推荐

  1. 『cs231n』无监督学习

    经典无监督学习 聚类 K均值 PCA主成分分析 等 深度学习下的无监督学习 自编码器 传统的基于特征学习的自编码器 变种的生成式自编码器 Gen网络(对抗式生成网络) 传统自编码器 原理 类似于一个自 ...

  2. hdu-1849-nim模板

    Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 蓝桥杯--乘积最大(数字DP)

    1230: 乘积最大 [DP] 时间限制: 1 Sec 内存限制: 128 MB 提交: 7 解决: 5 状态 题目描述 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚 ...

  4. priority_queue与bfs不得不说的古寺

    前几天写到bfs,看到之前写的,突然感觉不对,后来发现自己把点权值默认当成了边权值,导致一直走不出来: 点权值嘛,就是经过这个点时,要付出这么多的代价,边权值则是经过边时付出,二者有区别滴: 边权值求 ...

  5. http 请求和格式

    get 请求:从指定的资源请求数据. post请求:向指定的资源提交要被处理的数据. head请求:与 GET 相同,但只返回 HTTP 报头,不返回资源实体. option请求:返回服务器支持的 H ...

  6. OC 设计模式

    设计模式 一种或几种被所有程序员广泛认同的,有某些特定功能,或者实现某些特殊作用的编码格式 单例模式 键值编码(KVC) 键值观察(KVO) 观察者模式() 工厂模式(工厂方法) ps:MVC &am ...

  7. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...

  8. 深入理解$watch ,$apply 和 $digest --- 理解数据绑定过程——续

    Angular什么时候不会自动为我们$apply呢? 这是Angular新手共同的痛处.为什么我的jQuery不会更新我绑定的东西呢?因为jQuery没有调用$apply,事件没有进入angular ...

  9. spring boot 学习(七)小工具篇:表单重复提交

    注解 + 拦截器:解决表单重复提交 前言 学习 Spring Boot 中,我想将我在项目中添加几个我在 SpringMVC 框架中常用的工具类(主要都是涉及到 Spring AOP 部分知识).比如 ...

  10. BZOJ1670 [Usaco2006 Oct]Building the Moat护城河的挖掘

    裸的凸包...(和旋转卡壳有什么关系吗...蒟蒻求教T T) 话说忘了怎么写了...(我以前都是先做上凸壳再做下凸壳的说) 于是看了下hzwer的写法,用了向量的点积,方便多了,于是果断学习(Orz) ...