七、Web组件注入
1、Web原生组件注入(Servlet、Filter、Listener)
-
方式一:使用 @ServletComponentScan + web3.0注解的方式
-
在MainApplicationContext主启动类上加上ServletComponentScan("")注解
@ServletComponentScan("day01.view") @SpringBootApplication public class MainApplicationContext { public static void main(String[] args) { SpringApplication.run(ViewApplication.class, args); } }
-
分别在Servlet、Filter、Listener加上web3.0的注解
@WebServlet("/my") public class MyServlet extends HttpServlet { }
@WebFilter("/css/*") public class MyFilter implements Filter { }
@WebListener public class MyListener implements ServletContextListener { }
-
-
方式二:使用RegistrationBean
-
编写自己的web组件
//Servlet public class MyServlet extends HttpServlet { } //Filter public class MyFilter implements Filter { } //Listener public class MyListener implements ServletContextListener { }
-
编写一个配置类,用来注册这些Bean
@Configuration(proxyBeanMethods = true)//FilterRegistrationBean依赖Servlet相关 public class WebConfig { //注册Servlet @Bean public ServletRegistrationBean servletRegistration(){ MyServlet servlet = new MyServlet(); return new ServletRegistrationBean(servlet,"/my"); } //注册Filter @Bean public FilterRegistrationBean filterRegistrationBean(){ MyFilter filter = new MyFilter(); return new FilterRegistrationBean(filter,servletRegistration()); } //注册Listener @Bean public ServletListenerRegistrationBean servletListenerRegistrationBean(){ MyListener listener = new MyListener(); return new ServletListenerRegistrationBean(listener); } }
-
2、DispatcherServlet原理
-
要回答这个问题,首先要弄清楚DispatcherServlet是如何注入进来的
- 容器中自动配置了DispatcherServlet属性绑定到WebMvcProperties(配置项为spring.mvc)
- 通过ServletRegistrationBean<DispathcherServlet>把DispatcherServlet配置进啦
- 默认映射的是 / 路径
-
上述提到 DispatcherServlet的映射路径是 / ,Tomcat优先处理路径有关,看下图
-
Tomcat路径优先处理是:精确优选原则,(即,当多个Servlet都能处理统一层路径时,精确的路径优先)
-
这就解释了为什么原生Web api并不会被Spring的拦截器拦截的原因
3、嵌入式Servlet容器
-
默认支持的WebServer
- Tomcat Jetty Undertow Netty
- ServletWebServerApplicationContext 容器启动寻找ServletWebServerFactory,并引导创建服务器
-
如何切换服务器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!--上述配置:SpringBoot默认的Web容器是Tomcat,现将Tomcat去除--> <!--再引入自己想配置的服务器即可,具体可查看Spring官方文档--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
4、嵌入式Servlet容器原理
- 当SpringBoot应用启动发现当前是Web应用,web场景包-导入Tomcat
- web应用会创建一个Web版的IOC容器ServletWebServerApplicationContext
- ServletWebServerApplicationContext启动的时候寻找ServletWebServerFactory(Servlet的web服务器工厂)
- SpringBoot底层默认有多个WebServer工厂(TomcatServletWebServerFactory,JettyServletWebServerFactory等等)
- 底层有一个自动配置类ServletWebServerFactoryAutoConfiguration
- ServletWebServerFactoryAutoConfiguration导入了ServletWebServerFactoryConfiguration(配置类)
- ServletWebServerFactoryConfiguration配置类,根据动态判断系统中导入了那个Web服务器的包(默认web-starter导入tomcat包),容器中就有那个web容器的工厂类
- TomcatServletWebServerFactory创建出Tomcat服务器并启动,TomcatWebServer的构造器拥有初始化方法initialize() this.tomcat.start()
- 内嵌服务器,就是手动吧启动服务器的代码调用(tomcat核心jar包存在)
5、SpringBoot定制化原理
-
定制化配置的方式
-
修改核心配置文件(application.properties/application.yaml)
-
编写自定义的配置类 XxxConfiguration + @Bean替换,增加容器中默认组件,视图解析器
-
Web应用,编写一个配置类时效内WebMvcfigurer即可定制化web功能 结合 @Bean给容器中再扩展组件
@Configuration//标注这是一个控制类 public class WebConfig implements WebMvcConfigurer { }
-
@EnableWebMvc + WebMvcConfigurer 配合 @Bean 可以全面接管SpringMVC,所有的规则全部自己重新配置,实现定制和扩展功能
-
该种方式的原理
-
WebMvcAutoConfiguration 默认的SpringMVC的自动配置功能类(静态资源、欢迎页)
-
一旦使用@EnableWebMvc会导入DelegatingWebMvcConfiguration.class
@Import(DelegatingWebMvcConfiguration.class) public @interface EnableWebMvc { }
-
DelegatingWebMvcConfiguration的作用:只能保证SpringMVC最基本功能的使用
- 把系统中的WebMvcConfigurer拿来,所有功能的定制都是这些WebMvcConfigurer合起来一起生效
- 自动配置了一些非常底层的组件,RequestMappingHandlerMapping、这些组件依赖的组件都是从容器中获取
-
WebMvcAutoConfiguration里面的配置要能生效,必须@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
-
@EnableWebMvc导致了WebMvcAutoConfiguration没有生效
-
-
-
-
如何进行原理分析
- 引入场景starter -> XxxAutoConfiguration -> 导入Xxx组件 -> 绑定XxxProperties -> 绑定配置文件项