17、配置嵌入式servlet容器(1)
阿新 • • 發佈:2019-02-09
修改 utf ride listener web.xml map tor != ron 替代
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 publicvoid 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 = "/";
17、配置嵌入式servlet容器(1)