一、配置嵌入式Servlet容器
Spring Boot默认使用Tomcat作为嵌入式的Servlet容器
1)如何修改嵌入式Tomcat的相关配置
和server相关的配置,在org.springframework.boot.autoconfigure.web.ServerProperties.java类。
可通过配置文件来修改:
2)注册Servlet的三大原生组件【Servlet、Filter、Listener】
在war包形式中,是通过web.xml来注册这三大组件的。
但Spring Boot默认是以jar包的方式启动嵌入式的Servlet容器来启动Spring Boot的web应用,需要通过@Bean的方式注入
- 注册三大组件:
- 1、ServletRegistrationBean
- 2、FilterRegistrationBean
- 3、ServletListenerRegistrationBean
- 三大组件的一个最好实现就是DispatcherServlet:Spring Boot帮我们自动配置SpringMVC的时候,自动注册SpringMVC的前端控制器DispatcherServlet。
默认拦截/ 所有请求,包括静态资源,但不包括jsp请求; /*拦截所有请求,包括jsp
可以通过配置文件修改拦截路径。 - 注册三大组件的代码实现:
3)Spring Boot是否支持其他嵌入式容器
Spring Boot默认支持四种嵌入式容器,分别是Tomcat(默认使用)、Jetty、Netty、Undertow
- Tomcat(默认使用)
- Jetty(高性能非阻塞,适合高并发)
- Netty
- Undertow(适合长连接,如:web聊天)
- 如何引入:
Tomcat(默认使用)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
引入web模块默认就是使用嵌入式的Tomcat作为Servlet容器;
</dependency>
========================
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>
========================
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>
4)嵌入式Servlet容器的自动配置原理
步骤:
1、Spring Boot根据导入的依赖情况,决定要使用的容器是哪个(默认Tomcat)。
可以看到@ConditionalOnClass注解,意思是:如果有这个类,就进入此方法;导入了哪个容器的依赖,就可以得到对应的类。
5)嵌入式Servlet容器的启动原理
二、配置外置的Servlet容器
嵌入式Servlet容器:应用打成可执行jar
优点:简单便捷
缺点:不支持jsp、优化定制比较复杂(得懂原理才能重写)
外置的Servlet容器:应用打成可执行war,需手动安装Servlet容器
1)创建war项目
①将Packaging修改为war,其余与创建jar项目相同
②创建成功后,发现一个ServletInitializer类,且packaging为war
③创建webapp目录和web.xml页面
④把服务器整合进IDEA中
⑤编写一个jsp文件,测试是否可以访问。
默认的http://localhost:8080访问出错,因为没有可访问的首页面。
⑥与jar项目的不同有两点:
- 1、将嵌入式的Tomcat指定为provided。通过对比图可以明显看出来
- 2、必须编写一个SpringBootServletInitializer的子类,并调用configuer(...)方法。这是可以启动关键(直接运行SpringBoot04WebJsp02Application的main方法报错)。
2)使用外置的Servlet容器的原理
jar包:执行Spring Boot主类的main方法,启动ioc容器,再创建嵌入式的Servlet容器;--先启动SpringBoot应用,再启动Servlet容器
war包:启动服务器,服务器启动Spring Boot主类的main方法【SpringBootServletInitializer】,再启动ioc容器。--先启动Servlet容器,再启动SpringBoot应用
实现原理:servlet3.0的规范的: 8.2.4 Shared libraries / runtimes pluggability章节
大致描述流程:
1、启动服务器(Tomcat等)会创建当前web应用里面每一个jar包里面ServletContainerInitializer实例
2、ServletContainerInitializer的实现放在jar包的META-INF/services文件夹下,有一个名为javax.servlet.ServletContainerInitializer的文件,内容就是ServletContainerInitializer的实现类的全类名
3、xxx
4、运行onStartup方法
5、Spring的应用启动并且创建IOC容器