SpringBoot之配置嵌入式Servlet容器
2018-07-27 21:34
661 查看
1、SpringBoot默认使用Tomcat作为嵌入式的Servlet容器;
2、如何定制和修改Servlet容器的相关配置;有两种方式
1).修改和server有关的配置(ServerProperties【也是EmbeddedServletContainerCustomizer】);
[code]spring.mvc.date-format=yyyy-MM-dd spring.thymeleaf.cache=false spring.messages.basename=i18n.login server.port=8081 server.context-path=/crud server.tomcat.uri-encoding=UTF-8
通用的Servlet容器设置server.xxx Tomcat的设置server.tomcat.xxx
2).编写一个**EmbeddedServletContainerCustomizer**:嵌入式的Servlet容器的定制器;来修改Servlet容器的配置
3、注册servlet三大组件:servlet,filter,listener
MyServlet:
MyFilter:过滤器
MyListener:监听器
由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。注册三大组件用以下方式ServletRegistrationBean,FilterRegistrationBean,ServletListenerRegistrationBean
[code]@Configuration public class MyServerConfig { //注册三大组件 @Bean public ServletRegistrationBean myServlet(){ //映射/myServlet请求 ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet"); registrationBean.setLoadOnStartup(1); return registrationBean; } @Bean public FilterRegistrationBean myFilter(){ FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new MyFilter()); registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet")); return registrationBean; } @Bean public ServletListenerRegistrationBean myListener(){ ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener()); return registrationBean; } //配置嵌入式的Servlet容器 @Bean public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){ return new EmbeddedServletContainerCustomizer() { //定制嵌入式的Servlet容器相关的规则 @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8083); } }; } }
SpringBoot在配置SpringMVC的时候,自动的注册SpringMVC的前端控制器:DIspatcherServlet;所以不用配了
参考DispatcherServletAutoConfiguration类源码:
4、替换为其他嵌入式Servlet容器
默认为tomcat:
[code]<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> 引入web模块默认就是使用嵌入式的Tomcat作为Servlet容器; </dependency>
Jetty:
[code]<!-- 引入web模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <!--引入其他的Servlet容器--> <dependency> <artifactId>spring-boot-starter-jetty</artifactId> <groupId>org.springframework.boot</groupId> </dependency>
Undertow:
[code]<!-- 引入web模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <!--引入其他的Servlet容器--> <dependency> <artifactId>spring-boot-starter-undertow</artifactId> <groupId>org.springframework.boot</groupId> </dependency>
阅读更多
相关文章推荐
- 【spring-boot】spring-boot-配置嵌入式Servlet容器学习
- Spring Boot 学习笔记(五) 配置Servlet容器
- Spring实现原理分析(二十六).Spring Boot关于嵌入式servlet容器
- 【spring】SpringBoot之Servlet、Filter、Listener配置
- 在部署到Servlet容器之前,如何生成包含SpringBoot的War文件
- 常规容器下SpringBootServletInitializer如何实现web.xml作用解析
- SpringBoot深入(一)--SpringBoot内置web容器及配置
- springboot框架servlet容器tomcat、Jetty、undertow压测报告
- SpringBoot初始教程之Servlet、Filter、Listener配置(七)
- springboot不同servlet容器benchmark
- SpringBoot入门-12(springboot配置servlet,利用代码实现)
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- SpringBoot之Servlet、Filter、Listener配置
- 170322、Spring Boot 性能优化之将Servlet容器变成Undertow
- Spring Boot中的容器配置和SSL支持
- SpringBoot之在Servlet2.5容器中部署war应用
- SpringBoot初始教程之Servlet、Filter、Listener配置
- IntelliJ IDEA 配置Gradle运行SpringBoot Web项目(tomcat容器)
- springboot mvc 配置servlet filter lister
- SpringBoot入门-12-1(springboot配置servlet,利用注解实现)