在SpringBoot中,有一個默認的嵌入式Servlet容器——Tomcat容器
1、如何定製和修改Servlet容器的有關配置?
【1】在SpringBoot的配置文件中進行配置
# 修改服務器的端口號
server.port=8090
# 修改訪問路徑
server.servlet.context-path=/webDemo
# 設置Tomcat容器的字符編碼
server.tomcat.uri-encoding=UTF-8
# server.tomcat.xxx= 是對tomcat容器的設置
# server.xxx.xxx= 是通用的servlet容器設置
【2】手動編寫一個嵌入式的Servlet容器的定製器
① 1.x版本:使用EmbeddedServletContainerCustomizer
來修改Servlet容器的配置
@Configuration
public class MyMvcConfiguration extends WebMvcConfigurerAdapter {
//在容器中配置嵌入式Servlet容器
@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
return new EmbeddedServletContainerCustomizer(){
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8888);
}
};
}
}
② 2.x版本:使用ConfigurableServletWebServerFactory
來修改Servlet容器的配置
@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory(){
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.setPort(8010);
return factory;
}
2、在SpringBoot中註冊三大組件(Servlet、Filter、Listener)
【1】1.x版本
① 註冊Servlet
在容器中註冊自定義的Servlet:
@Configuration
public class MyServerConfiguration{
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
return servletRegistrationBean;
}
}
自定義的Servlet:
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.write("Hello,MyServlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
【2】註冊過濾器Filter
在容器中註冊自定義的過濾器:
@Configuration
public class MyServerConfiguration {
/*註冊過濾器Filter組件*/
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.setUrlPatterns(Arrays.asList("/myServlet","/hello"));
return filterRegistrationBean;
}
}
自定義的過濾器:
public class MyFilter implements Filter {
/* *
* @description //TODO 過濾器的初始化方法
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
/*
* @description //TODO 過濾器的執行方法
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("MyFiler is processing……");
chain.doFilter(servletRequest,servletResponse);
}
/* 過濾器的銷燬方法*/
@Override
public void destroy() {
}
}
【3】註冊監聽器(Listener)
在容器中註冊監聽器:
@Bean
public ServletListenerRegistrationBean myServletListener(){
ServletListenerRegistrationBean<MyServletListener> servletRegistrationBean = new ServletListenerRegistrationBean<MyServletListener>(new MyServletListener());
return servletRegistrationBean;
}
自定義的監聽器:
public class MyServletListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("web應用啓動……");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("web容器銷燬……");
}
}
可以註冊的監聽器:
static {
Set<Class<?>> types = new HashSet();
types.add(ServletContextAttributeListener.class);
types.add(ServletRequestListener.class);
types.add(ServletRequestAttributeListener.class);
types.add(HttpSessionAttributeListener.class);
types.add(HttpSessionListener.class);
types.add(ServletContextListener.class);
SUPPORTED_TYPES = Collections.unmodifiableSet(types);
}
SpringBoot會幫助我們自動註冊Spring MVC的前端控制器 (DispatcherServlet)
默認攔截(/):除jsp請求外,攔截所有的請求,包括靜態資源;(/*):會攔截jsp請求。
可以通過server.servletPath
來修改SpringMVC前端控制器默認攔截的請求路徑。
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public ServletRegistrationBean dispatcherServletRegistration(
DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet, this.serverProperties.getServletMapping());
registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
registration.setLoadOnStartup(
this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}
【2】2.x版本註冊方式與上面的相同。
3、使用其他Servlet容器
在SpringBoot中,默認使用Tomcat服務器,但是也可以切換成·Undertow和Jetty。
如何切換其他容器?
【1】先排除SpringBoot的默認容器Tomcat
<!--導入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>
【2】引入目標容器的依賴
① 切換成undertow容器:
<!--切換其他容器
1、先排除默認的tomcat容器
2、引入要切換的容器的座標
-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<!--<version>SpringBoot的版本號</version> -->
</dependency>
② 切換成Netty容器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<version>版本號</version>
</dependency>