配置springboot中嵌入式的Servlet容器
Springboot默认使用嵌入式的Servlet容器(Tomcat)
问题:
1)、如何定制和修改Servlet容器的相关配置
1)、通过配置文件修改和server相关的配置(serverProterties)
server.port=8000
server.servlet.context-path=/crud
//通用的servlet容器设置
server.xxx
//tomcat的设置
server.tomcat.xxx
2)、通过编写一个WebServerFactoryCustomizer:嵌入式的Servlet容器的定制器;来修改servlet容器的配置
// 配置嵌入式Servlet容器
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> myembeddedServletContainerCustomizer(){
return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>(){
// 定制嵌入式Servlet容器相关规则
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8081);
}
};
}
2)、注册Servlet三大组件(Servlet、Filter、Listener)
1)、注册Servlet组件
- 自定义一个servlet
// 自定义servlet
public class MyServlet extends HttpServlet {
//处理post请求
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello,myServlet");
}
// 处理get请求
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
}
-
注册Servlet
// 注册三大组件 @Bean public ServletRegistrationBean myServlet(){ ServletRegistrationBean servletregistrationbean = new ServletRegistrationBean(new MyServlet() ,"/myservlets"){}; return servletregistrationbean; }
2)、注册filter组件
-
自定义filter
public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("myfilter precess..."); } @Override public void destroy() { } }
-
注册filter
@Bean public FilterRegistrationBean myFilter(){ FilterRegistrationBean filters = new FilterRegistrationBean(); filters.setFilter(new MyFilter()); filters.setUrlPatterns(Arrays.asList("/hh","/myservlet")); return filters; }
3)、注册listener
-
自定义listener
public class MyListener implements ServletContextListener{ @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("myListener启动。。。服务启动"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("myListener关闭。。。该项目停止服务"); } }
-
注册listener
@Bean public ServletListenerRegistrationBean myListener(){ ServletListenerRegistrationBean<MyListener> myListenerServletListenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener()); return myListenerServletListenerRegistrationBean; }
项目启动后:
springboot帮我自动加载springMVC的时候,自动的注册SpringMVC的前端控制器;DispatcherServlet;
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, this.serverProperties.getServlet().getPath());
registration.setName("dispatcherServlet");
// 默认拦截: / 所有请求,包括静态资源,但是不拦截jsp请求;/*会拦截jsp
registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}
3)、springboot能不能支持其他的Servlet容器
tomcat(默认的容器)
Jetty(长连接;适合面对面聊天的工具)
Undertow(不支持JSP;并发的高性能容器)
更换Servlet容器
<dependency>
<!-- 引入web模块-->
<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>
更换servlet容器不会影响其他配置