springboot之配置嵌入式servlet容器

springboot默认使用tomcat作为嵌入式的servlet容器

问题:

1、如何定制和修改servlet容器的相关配置

(1)修改和server有关的配置(ServerProperties)

  server.port = 8081

server.servlet.context-path=/crud

  server.tomcat.uri-encoding = UTF-8

  //通用的servlet容器设置

  server.xxx

  // tomcat的设置

  server.tomcat.xxx

(2)编写一个WebServerFactoryCustomizer:嵌入式的servlet容器的定制器,来修改servlet容器的配置

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
       return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
           @Override
           public void customize(ConfigurableWebServerFactory factory) {
               factory.setPort(8083);
           }
       };
    }

==============注册servlet三大组件Servlet,Filter,Listener====================

由于springboot默认是以jar包的方式启动嵌入式的servlet容器来启动springboot的web应用,没有web.xml文件。

注册三大组件用以下方式:

ServletRegistrationBean

FilterRegistrationBean

ServletListenerRegistrationBean

(1)新建servlet包MyServlet类

package com.cnstrong.springboot04webrestfulcrud.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Hello Servlet");
    }
}

在config包下新建MyServerConfig类

package com.cnstrong.springboot04webrestfulcrud.config;

import com.cnstrong.springboot04webrestfulcrud.servlet.MyServlet;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyServerConfig {

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


    //配置嵌入式的servlet容器
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {

            //定制嵌入式的servlet容器相关的规则
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                factory.setPort(8083);
            }
        };
    }
}

(2)新建filter包MyFilter类

package com.cnstrong.springboot04webrestfulcrud.filter;

import javax.servlet.*;
import java.io.IOException;

public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("MyFilter process...");
        chain.doFilter(request,response);
    }
}
MyServerConfig类中新增方法
@Bean
public FilterRegistrationBean myFilter(){
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(new MyFilter());
    registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
    return registrationBean;
}

(3)新建listener包创建MyListener类

package com.cnstrong.springboot04webrestfulcrud.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized...web应用启动");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextDestroyed...当前web项目销毁");
    }
}
修改MyServletConfig

新增方法myListener

@Bean
public ServletListenerRegistrationBean myListener(){
    //alt+Enter    补全返回值ServletListenerRegistrationBean<MyListener> registrationBean
    ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
    return registrationBean;
}

修改方法myServlet

@Bean
public ServletRegistrationBean myServlet(){
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
    registrationBean.setLoadOnStartup(1);
    return registrationBean;
}

如:

springboot帮我们自动配置springmvc的时候,会自动注册springmvc的前端控制器,DispatcherServlet

DispatcherServletAutoConfiguration里
DispatcherServletConfiguration中
@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet() {
   DispatcherServlet dispatcherServlet = new DispatcherServlet();
   dispatcherServlet.setDispatchOptionsRequest(this.webMvcProperties.isDispatchOptionsRequest());
   dispatcherServlet.setDispatchTraceRequest(this.webMvcProperties.isDispatchTraceRequest());
   dispatcherServlet
         .setThrowExceptionIfNoHandlerFound(this.webMvcProperties.isThrowExceptionIfNoHandlerFound());
   dispatcherServlet.setEnableLoggingRequestDetails(this.httpProperties.isLogRequestDetails());
   return dispatcherServlet;
}
DispatcherServletRegistrationConfiguration中
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {

   //默认拦截 :  / 所有请求,包括静态资源,但是不拦截jsp请求        区别/*会拦截jsp
   //可以通过spring.mvc.servlet.path来修改springmvc前端控制器默拦截的请求路径
   DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
         this.webMvcProperties.getServlet().getPath());
   registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
   registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());
   if (this.multipartConfig != null) {
      registration.setMultipartConfig(this.multipartConfig);
   }
   return registration;
}
@ConfigurationProperties(prefix = "spring.mvc")
public class WebMvcProperties {中
public static class Servlet {

   /**
    * Path of the dispatcher servlet.
    */
   private String path = "/";

2、springboot能不能支持其他的servlet容器

默认支持

tomcat(默认使用)

Jetty(长连接)

Undertow(高并发)

3、替换为其他servlet容器

引入web模板默认就是使用嵌入式的tomcat作为servlet容器

先排除依赖

 

<!--引入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>
<!--引入其他的servlet容器-->
<dependency>
    <artifactId>spring-boot-starter-jetty</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>

<dependency>
    <artifactId>spring-boot-starter-undertow</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值