17、配置嵌入式servlet容器(1)
SpringBoot默认使用Tomcat作为嵌入式的Servlet容器
1)、如何定制和修改Servlet容器的相关配置
1、修改和server有关的配置
(ServerProperties【也是EmbeddedServletContainerCustomizer】)
server.port=8088 server.servlet.context-path=/crud //通用的Servlet容器设置 server.xxx //Tomcat的设置 server.tomcat.xxx server.tomcat.uri-encoding=UTF-8
2、编写一个EmbeddedServletContainerCustomizer:
嵌入式的Servlet容器的定制器;来修改Servlet容器的配置
在Spring Boot2.0以上配置嵌入式Servlet容器时
EmbeddedServletContainerCustomizer类不存在,
经网络查询发现被WebServerFactoryCustomizer替代
@Configuration public class config { @Bean public WebServerFactoryCustomizer webServerFactoryCustomizer(){ return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory >() { //定制嵌入式的Servlet容器相关的规则 @Override public void customize(ConfigurableServletWebServerFactory factory) { factory.setPort(8087); } }; } }
2)、注册Servlet、Filter、Listener
由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动
SpringBoot的web应用,没有web.xml文件
使用一下三个类:
ServletRegistrationBean
FilterRegistrationBean
ServletListenerRegistrationBean
注册Servlet
Servlet实现类:
public class MyServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("hello"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } }
ServletRegistrationBean实现Servl加入容器
@Configuration public class MyServletConfig { //注册三大组件 @Bean public ServletRegistrationBean servletRegistrationBean(){ // public ServletRegistrationBean(T servlet, String... urlMappings) ServletRegistrationBean re = new ServletRegistrationBean(new MyServlet(),"/myServlet"); return re; } }
/myServlet、是拦截浏览器的请求
注册Filter
public class MyFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("MyFilter..."); filterChain.doFilter(servletRequest,servletResponse); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
将Filter添加到容器
@Configuration public class MyServletConfig { //注册Filter @Bean public FilterRegistrationBean filterRegistrationBean(){ // public FilterRegistrationBean(T filter, ServletRegistrationBean... servletRegistrationBeans) FilterRegistrationBean f = new FilterRegistrationBean(); f.setFilter(new MyFilter()); f.setUrlPatterns(Arrays.asList("/myfilter","/test")); return f; } }
Listener:
public class MyListener implements ServletContextListener { //监听当前web项目销毁 @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("web end...."); } //启动监听 @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("web start..."); } }
加入容器
@Configuration public class MyServletConfig { //注册Listener @Bean public ServletListenerRegistrationBean servletListenerRegistrationBean(){ ServletListenerRegistrationBean<MyListener> L = new ServletListenerRegistrationBean<MyListener>(new MyListener()); return L; } }
同时可以设置的参数很多如load-starton......
SpringBoot帮我们自动SpringMVC的时候,自动的注册SpringMVC的前端控制器;
DIspatcherServlet;DispatcherServletAutoConfiguration中:
@Bean( name = {"dispatcherServletRegistration"} ) @ConditionalOnBean( value = {DispatcherServlet.class}, name = {"dispatcherServlet"} ) public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) { DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, this.webMvcProperties.getServlet().getPath()); //默认拦截: / 所有请求;包静态资源,但是不拦截jsp请求 // /*会拦截jsp //可以通过server.servletPath来修改SpringMVC前端控制器默认拦截的请求路径 registration.setName("dispatcherServlet"); registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup()); if (this.multipartConfig != null) { registration.setMultipartConfig(this.multipartConfig); } return registration; }
getPath->WebMvcProperties -> private String path = "/";