SpringBoot默认使用Tomcat作为嵌入式的Servlet容器
如何定制和修改Servlet容器的相关配置;
1、修改和server有关的配置
server.port=8081
server.context-path=/crud
server.tomcat.uri-encoding=UTF-8
//通用的Servlet容器设置
server.xxx
//Tomcat的设置
server.tomcat.xxx
2、编写一个WebServerFactoryCustomizer:嵌入式的Servlet容器的定制器;来修改Servlet容器的配置
在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类不存在,经网络查询发现被WebServerFactoryCustomizer替代。
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
@Override
public void customize(ConfigurableWebServerFactory factory) {
//配置端口号
factory.setPort(8083);
}
};
}
注册Servlet三大组件【Servlet、Filter、Listener】
由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。
编写一个Servlet类:注册到容器当中:ServletRegistrationBean
@Bean//加到容器当中
public ServletRegistrationBean myServlet(){
ServletRegistrationBean<MyServlet> myServletServletRegistrationBean = new ServletRegistrationBean<>(new MyServlet(), "/MyServlet");
return myServletServletRegistrationBean;
}
编写一个Filter类:注册到容器当中:FilterRegistrationBean
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
filterFilterRegistrationBean.setFilter(new MyFilter());
//拦截请求
filterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/hello","/myfilter"));
return filterFilterRegistrationBean;
}
编写一个Listener类:注册到容器当中:ServletListenerRegistrationBean
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean<MyListener> myListenerServletListenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener());
return myListenerServletListenerRegistrationBean;
}
SpringBoot帮我们自动SpringMVC的时候,自动的注册SpringMVC的前端控制器;DIspatcherServlet;
@Bean(
name = {"dispatcherServletRegistration"}
)
@ConditionalOnBean(
value = {DispatcherServlet.class},
name = {"dispatcherServlet"}
)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet, WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, webMvcProperties.getServlet().getPath());
registration.setName("dispatcherServlet");
registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
multipartConfig.ifAvailable(registration::setMultipartConfig);
return registration;
}
}
默认拦截: / 所有请求;包静态资源,但是不拦截jsp请求; /*会拦截jsp
可以通过server.servletPath来修改SpringMVC前端控制器默认拦截的请求路径
切换为其他嵌入式Servlet容器
默认支持:Tomcat、Jetty、Undertow,新版本SpringBoot中还添加了Netty新机制容器;默认使用tomcat
需要切换只需要导入对应的pom.xml文件即可
1、Tomcat(默认)
2、SpringBoot支持Jetty(长连接)
3、也支持Undertow(不支持Jsp:并发性能比较好)
springboot里面的Tomcat是在spring-boot-starter-web下,
所以我们如果需要切换服务器的话,需要先移除了spring-boot-starter-web里面的Tomcat依赖,再建立你想要切换的服务器。
<!--引入web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除Tomcat starter-->
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引入其他的jetty starter容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>