【转】spring boot web相关配置
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注解修改),创建方式如下:
- @Component("myServlet")
- public class MyServlet implements Servlet{
- /**
- *
- * @see javax.servlet.Servlet#destroy()
- */
- @Override
- public void destroy() {
- System.out.println("destroy...");
- }
- /**
- * @return
- * @see javax.servlet.Servlet#getServletConfig()
- */
- @Override
- public ServletConfig getServletConfig() {
- return null;
- }
- /**
- * @return
- * @see javax.servlet.Servlet#getServletInfo()
- */
- @Override
- public String getServletInfo() {
- return null;
- }
- /**
- * @param arg0
- * @throws ServletException
- * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
- */
- @Override
- public void init(ServletConfig arg0) throws ServletException {
- System.out.println("servlet init...");
- }
- /**
- * @param arg0
- * @param arg1
- * @throws ServletException
- * @throws IOException
- * @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
- */
- @Override
- public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException,
- IOException {
- System.out.println("service...");
- }
2. filter配置
filter配置的映射是/*,创建方式如下:
- @Component("myFilter")
- public class MyFilter implements Filter{
- /**
- *
- * @see javax.servlet.Filter#destroy()
- */
- @Override
- public void destroy() {
- System.out.println("destroy...");
- }
- /**
- * @param arg0
- * @param arg1
- * @param arg2
- * @throws IOException
- * @throws ServletException
- * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
- */
- @Override
- public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
- throws IOException,
- ServletException {
- System.out.println("doFilter...");
- arg2.doFilter(arg0, arg1);
- }
- /**
- * @param arg0
- * @throws ServletException
- * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
- */
- @Override
- public void init(FilterConfig arg0) throws ServletException {
- System.out.println("filter init...");
- }
- }<span style="font-family: Arial, Helvetica, FreeSans, sans-serif; font-size: 13.3333330154419px; line-height: 17.3333339691162px; background-color: transparent;"> </span>
3. listener配置
- @Component("myListener")
- public class MyListener implements ServletContextListener{
- /**
- * @param arg0
- * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
- */
- @Override
- public void contextDestroyed(ServletContextEvent arg0) {
- System.out.println("contextDestroyed...");
- }
- /**
- * @param arg0
- * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
- */
- @Override
- public void contextInitialized(ServletContextEvent arg0) {
- System.out.println("listener contextInitialized...");
- }
- }
如果觉得控制力度不够灵活(例如你想修改filter的映射),spring boot还提供了 ServletRegistrationBean,FilterRegistrationBean,ServletListenerRegistrationBean这3个东西来进行配置
修改filter的映射
- @Configuration
- public class WebConfig {
- @Bean
- public FilterRegistrationBean filterRegistrationBean(MyFilter myFilter){
- FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
- filterRegistrationBean.setFilter(myFilter);
- filterRegistrationBean.setEnabled(true);
- filterRegistrationBean.addUrlPatterns("/bb");
- return filterRegistrationBean;
- }
- }
4. 配置servlet 容器
可以通过两种方式配置servlet容器,一种是通过properties文件,例如:
- server.port=8081
- server.address=127.0.0.1
- server.sessionTimeout=30
- server.contextPath=/springboot
另一种方式是Java代码的形式:
- @Component
- public class MyCustomizationBean implements EmbeddedServletContainerCustomizer {
- /**
- * @param container
- * @see org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer#customize(org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer)
- */
- @Override
- public void customize(ConfigurableEmbeddedServletContainer container) {
- container.setContextPath("/sprintboot");
- container.setPort(8081);
- container.setSessionTimeout(30);
- }
- }
5. error 处理
spring boot 提供了/error映射来进行错误处理,它会返回一个json来对错误信息进行描述(包含了http状态和异常信息),例如404的错误
当然可以定制错误页面,通过实现ErrorController接口,并将它装配起来即可,如下:
- @Controller
- public class ErrorHandleController implements ErrorController {
- /**
- * @return
- * @see org.springframework.boot.autoconfigure.web.ErrorController#getErrorPath()
- */
- @Override
- public String getErrorPath() {
- return "/screen/error";
- }
- @RequestMapping
- public String errorHandle(){
- return getErrorPath();
- }
- }
还可以这样:
- @Component
- private class MyCustomizer implements EmbeddedServletContainerCustomizer {
- @Override
- public void customize(ConfigurableEmbeddedServletContainer container) {
- container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/screen/error"));
- }
- }
6.模板引擎
spring boot 会自动配置 FreeMarker,Thymeleaf,Velocity,只需要在pom中加入相应的依赖即可,例如应用Velocity
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-velocity</artifactId>
- </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相关配置的更多相关文章
- 转-spring boot web相关配置
spring boot web相关配置 80436 spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何w ...
- spring boot web相关配置
spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何web相关的配置便能提供web服务,这还得归于spri ...
- Spring Boot SSL [https]配置例子
前言 本文主要介绍Spring Boot HTTPS相关配置,基于自签证书实现: 通过本例子,同样可以了解创建SSL数字证书的过程: 本文概述 Spring boot HTTPS 配置 server. ...
- Springboot 系列(六)Spring Boot web 开发之拦截器和三大组件
1. 拦截器 Springboot 中的 Interceptor 拦截器也就是 mvc 中的拦截器,只是省去了 xml 配置部分.并没有本质的不同,都是通过实现 HandlerInterceptor ...
- Spring Boot 2.0 配置图文教程
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...
- Springboot 系列(五)Spring Boot web 开发之静态资源和模版引擎
前言 Spring Boot 天生的适合 web 应用开发,它可以快速的嵌入 Tomcat, Jetty 或 Netty 用于包含一个 HTTP 服务器.且开发十分简单,只需要引入 web 开发所需的 ...
- spring boot @ConditionalOnxxx相关注解总结
Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...
- Spring Boot 外部化配置(一)- Environment、ConfigFileApplicationListener
目录 前言 1.起源 2.外部化配置的资源类型 3.外部化配置的核心 3.1 Environment 3.1.1.ConfigFileApplicationListener 3.1.2.关联 Spri ...
- Spring Boot的自动配置
Spring Boot的自动配置 --摘自https://www.hollischuang.com/archives/1791 随着Ruby.Groovy等动态语言的流行,相比较之下Java的开发显得 ...
随机推荐
- golang学习之interface与其它类型转换
如下函数,将interface变量in转换为int: func formatTimeStamp(in interface{}, layout string) (out string) { timeSt ...
- nodejs中引入art-template模板
使用Webstorm创建nodejs express应用时,默认使用的是jade或者ejs模板,对于不习惯这两种模板语法的人来说确实不是很方便.没关系,这里我们使用art-template模板引擎,使 ...
- 二:Vim常用命令
一般模式下的命令: -- 插入命令 i 光标前插入 I 当前行开始 o 下一行 O 上一行插入新行 a 光标后插入 A 当前行末尾 -- 定位命令 :set nu 显示行号 :set nonu 取消行 ...
- java类与对象基础--抽象类和接口
在java的类体系中,有两个比较特殊的抽象体--抽象类和接口.抽象体并不可以拥有具体的对象(而且接口甚至不是类),但是它们却是在java设计领域非常重要的两个概念,很多优秀的设计模式都是基于这两个概念 ...
- springboot aop使用介绍
第一步:添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- Jvm性能监控和常用工具
JDK常用命令行工具 Jps : jps [options] [hostid] , -q 只显示jvmid, -m 传递给主类main的参数,-l 类全名,-v jvm启动参数 jstat : ...
- (转)快速了解微信小程序的使用,一个根据小程序的框架开发的todos app
微信官方已经开放微信小程序的官方文档和开发者工具.前两天都是在看相关的新闻来了解小程序该如何开发,这两天官方的文档出来之后,赶紧翻看了几眼,重点了解了一下文档中框架与组件这两个部分,然后根据简易教程, ...
- 转动的八卦图纯css实现
这类的东西网上一搜就是大把的,看着比较空旷的博客,所以自己也来写一个. <!DOCTYPE html> <html> <head> <meta chars ...
- 把getJson() 设置为同步执行
因为业务需求,需要在获取到json 数据后,对数据进行处理. 这时候,我们需要把getJson() 的方法设置为同步 $.ajaxSettings.async = false; getJson() 方 ...
- Java设计模式—观察者模式
观察者模式(Observer Pattern)也叫做发布订阅模式(Publish/subscribe). 其定义如下: 定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都 ...