SpringBoot配置嵌入式Servlet容器

SpringBoot默认使用Tomcat作为嵌入式的Servlet容器

如何定制和修改Servlet容器的相关配置;

1、修改和server有关的配置

server.port=8081
server.context-path=/crud

server.tomcat.uri-encoding=UTF-8

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

2、编写一个WebServerFactoryCustomizer:嵌入式的Servlet容器的定制器;来修改Servlet容器的配置

在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类不存在,经网络查询发现被WebServerFactoryCustomizer替代。

@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文件。

编写一个Servlet类:注册到容器当中:ServletRegistrationBean

@Bean//加到容器当中
public ServletRegistrationBean myServlet(){
    ServletRegistrationBean<MyServlet> myServletServletRegistrationBean = new ServletRegistrationBean<>(new MyServlet(), "/MyServlet");
    return myServletServletRegistrationBean;
}

编写一个Filter类:注册到容器当中:FilterRegistrationBean

@Bean
public FilterRegistrationBean myFilter(){
    FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
    filterFilterRegistrationBean.setFilter(new MyFilter());
    //拦截请求
    filterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/hello","/myfilter"));
    return filterFilterRegistrationBean;
}

编写一个Listener类:注册到容器当中:ServletListenerRegistrationBean

    @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean<MyListener> myListenerServletListenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return myListenerServletListenerRegistrationBean;
    }

SpringBoot帮我们自动SpringMVC的时候,自动的注册SpringMVC的前端控制器;DIspatcherServlet;

@Bean(
   name = {"dispatcherServletRegistration"}
)
@ConditionalOnBean(
   value = {DispatcherServlet.class},
   name = {"dispatcherServlet"}
)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet, WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
   DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, webMvcProperties.getServlet().getPath());
   registration.setName("dispatcherServlet");
   registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
   multipartConfig.ifAvailable(registration::setMultipartConfig);
   return registration;
}
}

在这里插入图片描述

默认拦截: / 所有请求;包静态资源,但是不拦截jsp请求; /*会拦截jsp
可以通过server.servletPath来修改SpringMVC前端控制器默认拦截的请求路径

切换为其他嵌入式Servlet容器

默认支持:Tomcat、Jetty、Undertow,新版本SpringBoot中还添加了Netty新机制容器;默认使用tomcat

需要切换只需要导入对应的pom.xml文件即可

1Tomcat(默认)

2、SpringBoot支持Jetty(长连接)

3、也支持Undertow(不支持Jsp:并发性能比较好)

springboot里面的Tomcat是在spring-boot-starter-web下,
所以我们如果需要切换服务器的话,需要先移除了spring-boot-starter-web里面的Tomcat依赖,再建立你想要切换的服务器。


<!--引入web模块-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    
    <!--排除Tomcat starter-->
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
    
</dependency>


<!--引入其他的jetty starter容器-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值