SpringBoot开发中的组件和容器详解
作者:韩_师兄
这篇文章主要介绍了SpringBoot开发中的组件和容器详解,SpringBoot 提供了一个内嵌的 Tomcat 容器作为默认的 Web 容器,同时还支持其他 Web 容器和应用服务器,需要的朋友可以参考下
Web原生组件
Web原生组件包括: Servlet、Filter、Listener等
相关使用
// 指定原生Servlet组件都放在那里 @ServletComponentScan(basePackages = "com.cf.admin") // 效果:直接响应,没有经过Spring的拦截器 @WebServlet(urlPatterns = "/my") @WebFilter(urlPatterns={"/css/*","/images/*"}) @WebListener
关于DispatchServlet 如何注册:
- 容器中自动配置了 DispatcherServlet 属性绑定到 WebMvcProperties;对应的配置文件配置项是 spring.mvc
- 通过 ServletRegistrationBean <DispatcherServlet> 把 DispatcherServlet 配置进来
- 默认映射的是 / 路径
Tomcat-Servlet中 多个Servlet都能处理到同一层路径,精确优选原则
- /my/
- /my/1
RegistrationBean使用
ServletRegistrationBean , FilterRegistrationBean , and ServletListenerRegistrationBean
@Configuration public class MyRegistConfig { @Bean public ServletRegistrationBean myServlet(){ MyServlet myServlet = new MyServlet(); return new ServletRegistrationBean(myServlet,"/my","/my02"); } @Bean public FilterRegistrationBean myFilter(){ MyFilter myFilter = new MyFilter(); // return new FilterRegistrationBean(myFilter,myServlet()); FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter); filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*")); return filterRegistrationBean; } @Bean public ServletListenerRegistrationBean myListener(){ MySwervletContextListener mySwervletContextListener = new MySwervletContextListener(); return new ServletListenerRegistrationBean(mySwervletContextListener); } }
Servlet容器
切换Servlet
- Spring Boot默认支持的webServer容器:
- Tomcat , Jetty , or Undertow 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应用。web场景包-导入tomcat
- web应用会创建一个web版的ioc容器 ServletWebServerApplicationContext
- ServletWebServerApplicationContext 启动的时候寻找 ServletWebServerFactory(Servlet 的web服务器工厂—> Servlet 的web服务器)
- SpringBoot底层默认有很多的WebServer工厂;TomcatServletWebServerFactory, JettyServletWebServerFactory, or UndertowServletWebServerFactory
- 底层直接会有一个自动配置类。ServletWebServerFactoryAutoConfiguration
- ServletWebServerFactoryAutoConfiguration导入了ServletWebServerFactoryConfiguration(配置类)
- ServletWebServerFactoryConfiguration 配置类 根据动态判断系统中到底导入了那个Web服务器的包。(默认是web-starter导入tomcat包),容器中就有 TomcatServletWebServerFactory
- TomcatServletWebServerFactory 创建出Tomcat服务器并启动;TomcatWebServer 的构造器拥有初始化方法initialize---this.tomcat.start();
- 内嵌服务器,就是手动把启动服务器的代码调用(tomcat核心jar包存在)
定制Servlet容器
1 实现 WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> 把配置文件的值和ServletWebServerFactory 进行绑定
2 修改配置文件servce.xxx, 直接自定义 ConfigurableServletWebServerFactory
import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.stereotype.Component; @Component public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Override public void customize(ConfigurableServletWebServerFactory server) { server.setPort(9000); } }
到此这篇关于SpringBoot开发中的组件和容器详解的文章就介绍到这了,更多相关SpringBoot组件和容器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!