配置嵌入式servlet容器

注: 参考视频

springboot默认是用的Tomcat作为嵌入式的servlet容器;

问题?

之前: 外置的Tomcat需要在外面Tomcat的conf的server.xml,webxml..配置文件进行修改

现在如何修改?

在配置文件.properties或者.yml 修改和server有关的配置, server.xx

能否支持其他的, 编写一个EmbeddedServletContainerCustomizer:嵌入式servlet容器的定制器;来修改servlet容器的配置

现在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类被WebServerFactoryCustomizer替代

//定制嵌入式的servlet容器
    //在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类被WebServerFactoryCustomizer替代
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>(){
            //定制嵌入式的servlet容器相关的规则
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                factory.setPort(8083);
            }
        };
    }

注册servlet,Filter,Listener

创建一个servlet包

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

注册三大组件用以下方式

ServletRegistrationBean

package com.lianxi.springboot.config;

import com.lianxi.springboot.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;

//和servlet有关的配置都在这个里面
@Configuration  //表示这个是配置类
public class MyServletConfig {
    //注册三大组件
    @Bean //加载容器中
    public ServletRegistrationBean myServlet(){
        //传入自己的servlet,/myServlet映射请求,发送/myServlet请求来到MyServlet, 访问http://localhost:8083/myServlet
        ServletRegistrationBean registrationBean=new ServletRegistrationBean(new MyServlet(),"/myServlet");
        //设置启动顺序
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }
}

FilterRegistrationBean

//注册Filter组件
    @Bean //加在容器中
    public FilterRegistrationBean myFilter(){
        //传入自己的Filter,
        FilterRegistrationBean registrationBean=new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        //发送/hello和/myServlet都会拦截,并且打印
        registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return registrationBean;
    }

ServletListenerRegistrationBean

//注册listener组件
    @Bean //加在容器中
    public ServletListenerRegistrationBean myListener(){
        //传入自己的Listener,
        ServletListenerRegistrationBean registrationBean=new ServletListenerRegistrationBean<MyListener>(new MyListener());
        return registrationBean;
    }

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

DispatcherServletAutoConfiguration类中

使用其他servlet容器

默认是Tomcat

<!--引入web模块-->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
     <!--引入web模块默认就是Tomcat容器作为servlet容器的-->
</dependency>

springboot也支持

jetty(长连接,比如点对点聊天需要一直架起连接)

undertow(不支持jsp)并发性能好

如何切换为其他的容器

排除掉Tomcat容器换其他的

排除后的pom.xml

引入jetty

       <!--引入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>
        <!--引入其他的servlet容器結束-->

引入undertow,必须是小写

和上面一样

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

嵌入式servlet容器自动配置原理

EmbeddedWebServerFactoryCustomizerAutoConfiguration: 嵌入式的servlet容器自动配置

2.0版本之前是EmbeddedServletContainerAutoConfiguration.java类, 2.0之后是EmbeddedWebServerFactoryCustomizerAutoConfiguration.java类

1)ReactiveWebServerFactory (嵌入式servlet容器工厂)

2.x之前是EmbeddedServletContainerFactory, 2.x之后是ServletWebServerFactory

2.x之前以TomcatcatEmbeddedServletContainerFactory伪例 ;  2.x之后是TomcatServletWebServerFactory的getWebServer方法

步骤:

springboot给容器添加了一个嵌入式的servlet容器工厂组件

嵌入式servlet容器启动原理

使用外置的servlet容器

嵌入式Servlet容器:应用打成可执行的jar

优点:简单、便携;

缺点:默认不支持JSP、优化定制比较复杂(使用定制器【ServerProperties、自定义EmbeddedServletContainerCustomizer(2.x以上WebServerFactoryCustomizer)】,自己编写嵌入式Servlet容器的创建工厂【EmbeddedServletContainerFactory】(2.x以后ServletWebServerFactory));

 

 

外置的Servlet容器:外面安装Tomcat---应用war包的方式打包;

例子: 创建一个新的项目

pom.xml是以war包的方式,src->main下需要一个webapp文件, 2种方式,一个是自己加文件夹, 一个是用idea

原理

jar包:执行SpringBoot主类的main方法,启动ioc容器,创建嵌入式的Servlet容器;

war包:启动服务器,服务器启动SpringBoot应用【SpringBootServletInitializer】,启动ioc容器;

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值