SpringBoot4-1---嵌入式Servlet容器

之前的web 项目中:将web 项目打成war 包,在web 外部配置一个tomcat(服务器/Servlet容器) 环境,将war 包放入服务器的指定目录下在外部启动服务器

Spring Boot中默认的是嵌入式的servlet 容器(Tomcat)
在这里插入图片描述

一:如何定制和修改Servlet容器中的相关配置:

1. 在application.properties 修改server 相关的配置:
 server.port=8081
server 属性对应一个类:ServerProperties
@ConfigurationProperties(
    prefix = "server",
    ignoreUnknownFields = true
)
public class ServerProperties {...}

★★★通用配置:server.xxx                           server.port=8081
★★★配置tomcat 的相关信息:server.tomcat.xxx        server.tomcat.uri-encoding=utf-8
2. 在配置类中写一个嵌入式Servlet 容器修改定制器:WebServerFactoryCustomizer;来修改和定制Servlet 容器配置

 创建Servlet容器,自定义修改配置组件
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> MyCustomizer(){
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            //定制嵌入的Servlet 容器

            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                factory.setPort(8081);       修改服务器的端口
            }
        };
    }

虽然这两种方式的表现形式不一样但是在本质上是一样的
在SpringBoot 2.0以下的版本中创建:EmbeddedServletContainerCustomizer 实现定制;但是2.0版本以上它就不存在了,被:WebServerFactoryCustomizer 替代

在SpringBoot 中有许多的xxxConfigrer 提供扩展配置
在SpringBoot 中有许多的xxxCustomizer 提供定制修改和配置


二:注册Servlet容器三大组件:Servlet Filter Listener

在基本的web 程序中目录结构为: mian/webapp/WEB-INF/web.xml 我们将所有的组件都注册在web.xml 文件中
在Spring Boot 中默认是以jar 包的形式启动嵌入Servlet 容器来启动SpringBoot 的web 应用,所以通过以下的方式注册Servlet容器的三大组件:
在一个统一的@Conguration 配置类中;注册Servlet 组件,组件的相关信息都可以在创建的方法进行配置(注册顺序等)
ServletRegistrationBean

public class Myservlet extends HttpServlet {
    注册Servlet
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        指定注册的Servlet  和映射的路径
    return new ServletRegistrationBean(new Myservlet(),"/servlet");  
    }

FilterRegistrationBean

public class MyFilter implements Filter {
注册Filter
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        在构造的时候指定使用的Filter 和 拦截的Servlet
        //FilterRegistrationBean filterRegistrationBean =  new FilterRegistrationBean(new MyFilter(),new ServletRegistrationBean<>(new Myservlet(),"/servlet"))
        
        	分步设置
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
       	 	设置使用的filter
        filterRegistrationBean.setFilter(new MyFilter());
        	设置过滤的请求
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/servlet","/hello"));
        return filterRegistrationBean;
    }

ServletListenerRegistrationBean

public class MyListener  implements ServletContextListener{
注册Listener
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
        return new ServletListenerRegistrationBean<MyListener>(new MyListener());
    }

SpringBoot 自动配置SpringMVC 时,自动注册了SpringMVC前端控制器:DispatcherServlet
(在类:DiapatcherServletAutoConfiguration 中的servletregistrationBean 方法中,注册还是使用ServletRegistrationBean 和其他的Servlet 注册一致)
return new ServletRegistrationBean(new DispatcherServlet(), urlPath);
注册的映射是:/ 所有的访问,包括静态资源,但是不包括jsp ;/* 是包括了所有的访问


三:如何使用其他的Servlet容器

SpringBoot默认Servlet容器使用:Tomcat
同时还配置了;
Jetty
Undetow
在使用:WebServerFactoryCustomizer创建自定义修改配置组件的时候,在 public void customize(ConfigurableWebServerFactory factory) 方法中进行自定义的设置和修改;关于ConfigurableWebServerFactory的继承树
在这里插入图片描述
所以在SpringBoot 中也可以使用Jetty 和Undertow

默认使用Tomcat 的原因:在导入web 启动器的时候,默认导入的是tomcat 的依赖;如果要使用其他的Servlet容器:将tomcat d的依赖排除。导入Jetty 或者 Undetow 的依赖

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值