SpringBoot自学好几天 中途开始写笔记 SpringBoot Web开发 配置嵌入式Servlet容器 20190129

配置嵌入式Servlet容器

SpringBoot默认使用的是嵌入式的Servlet容器(Tomcat)
SpringBoot自学好几天 中途开始写笔记 SpringBoot Web开发 配置嵌入式Servlet容器 20190129_第1张图片

1.如何定制和修改Servlet容器相关配置

方法1. 修改server相关配置(ServerProperties类中的属性)

server.port=8088
server.servlet.context-path=/tt
server.tomcat.uri-encoding=UTF-8

//通用Servlet容器设置
server.xxx
//tomcat设置
server.tomcat.xxx

方法2:编写一个EmbeddedServletContainerCustomizer
SpringBoot 2.0 以后改成了 WebServerFactoryCustomizer

  //配置嵌入式servlet服务器
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            //定制嵌入式容器相关规则
            public void customize(ConfigurableWebServerFactory factory) {
                factory.setPort(8888);
            }
        };
    }
2.注册Servlet Filter Listener

以下方法:

  • ServletRegistrationBean
public class MyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("MyServlet");
    }
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

//注册三大组件
	//@Configuration  	public class MyServerConfig { 中
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        return registrationBean;
    }

  • FilterRegistrationBean
	public class MyFilter implements javax.servlet.Filter {
	    @Override
	    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
	        System.out.println("MyFilter doFilter");
	        filterChain.doFilter(servletRequest,servletResponse);
	    }
	}
	//@Configuration  	public class MyServerConfig { 中
   @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return registrationBean;
    }
  • ServletListenerRegistrationBean
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized   应用启动了.....");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextDestroyed     应用销毁了.....");
    }
}

	//@Configuration  	public class MyServerConfig { 中
	 @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean<MyListener> listenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return listenerRegistrationBean;
    }

SpringBoot帮我们自动配置SpringMVC的时候,自动注册了SpringMVC的前端控制器DispatcherServlet

  @Bean(
            name = {"dispatcherServletRegistration"}
        )
        @ConditionalOnBean(
            value = {DispatcherServlet.class},
            name = {"dispatcherServlet"}
        )
        public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {
        	//默认拦截: / 所有资源 包括静态资源 不包括.jsp  /* 包括.jsp
        	//可以通过server.servlet.context-path 来修改前端控制器默认拦截的请求路径
            DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, this.webMvcProperties.getServlet().getPath());
            registration.setName("dispatcherServlet");
            registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());
            if (this.multipartConfig != null) {
                registration.setMultipartConfig(this.multipartConfig);
            }

            return registration;
        }
3.SpringBoot支持其他Servlet容器

SpringBoot自学好几天 中途开始写笔记 SpringBoot Web开发 配置嵌入式Servlet容器 20190129_第2张图片
SpringBoot默认支持
Tomcat(默认使用)

 <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

Jetty(长连接)

  1. pom.xml 排除Tomcat
 <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                    <groupId>org.springframework.bootgroupId>
                exclusion>
            exclusions>
        dependency>

2.pom.xml 引入其他Servlert容器

 
        <dependency>
            <artifactId>spring-boot-starter-jettyartifactId>
            <groupId>org.springframework.bootgroupId>
        dependency>
     

Undertow (不支持jsp)

  1. pom.xml 排除Tomcat
 <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                    <groupId>org.springframework.bootgroupId>
                exclusion>
            exclusions>
        dependency>

2.pom.xml 引入其他Servlert容器

 
         <dependency>
            <artifactId>spring-boot-starter-undertowartifactId>
            <groupId>org.springframework.bootgroupId>
        dependency>

你可能感兴趣的:(SpringBoot)