微服务架构Day05-SpringBoot之Servlet

旧版 配置嵌入式Servlet容器

SpringBoot默认使用Tomcat作为嵌入式Servlet容器

如何定制和修改Servlet容器相关配置
1.在配置文件中定制和修改Servlet容器有关的配置,本质上是使用SpringBoot的默认的嵌入式Servlet容器的定制器来修改配置.

通用的servlet容器配置 server.xx= 通用的Tomcat配置 server.tomcat.xx=

2.编写一个嵌入式Servlet容器定制器来修改Servlet容器的配置
在SpringBoot中会有xxxCustomizer来进行扩展配置,注意学习!!

注册Servlet三大组件(Servlet,Filter,Listener)

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

注册三大组件方式:
1.ServletRegistrationBean

@Configuration public class MyServerConfig { // 注册Servlet组件 @Bean public ServletRegistrationBean myServlet(){ ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(new MyServlet(),"/my"); return servletRegistrationBean; } }

2.FilterRegistrationBean

@Bean public FilterRegistrationBean myFilter(){ FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(); filterRegistrationBean.setFilter(new MyFilter()); filterRegistrationBean.setUrlPatterns(Arrays.asList("/my")); return filterRegistrationBean; }

3.ServletListenerRegistrationBean

@Bean public ServletListenerRegistrationBean myListener(){ ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener()); return registrationBean; }

SpringBoot自动配置SpringMVC时,自动注册SpringMVC的前端控制器:DispatcherServlet.

@Configuration @Conditional({DispatcherServletAutoConfiguration.DispatcherServletRegistrationCondition.class}) @ConditionalOnClass({ServletRegistration.class}) @EnableConfigurationProperties({WebMvcProperties.class}) @Import({DispatcherServletAutoConfiguration.DispatcherServletConfiguration.class}) protected static class DispatcherServletRegistrationConfiguration { private final WebMvcProperties webMvcProperties; private final MultipartConfigElement multipartConfig; public DispatcherServletRegistrationConfiguration(WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfigProvider) { this.webMvcProperties = webMvcProperties; this.multipartConfig = (MultipartConfigElement)multipartConfigProvider.getIfAvailable(); } @Bean( name = {"dispatcherServletRegistration"} ) @ConditionalOnBean( value = {DispatcherServlet.class}, name = {"dispatcherServlet"} ) public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) { DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, this.webMvcProperties.getServlet().getPath()); //可以通过修改servletpath来修改SpringMVC前端控制器默认拦截的请求路径 registration.setName("dispatcherServlet"); registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup()); if (this.multipartConfig != null) { registration.setMultipartConfig(this.multipartConfig); } return registration; } }

" / ":表示拦截所有请求,包括静态资源,但是不拦截jsp请求," /* ":表示拦截所有请求,包括jsp请求

注入Bean的几种方式:

@Bean注解

包扫描:

@Controller

@Service

@Repository

@Component

@Import:

实现ImportSelector接口的类

实现ImportBeanDefinitionRegistry接口

实现FactoryBean

SpringBoot支持其它的Servlet容器

默认支持:Tomcat(默认),Jetty,Undertow

Tomcat是最稳定的服务器,一般情况下推荐使用

Jetty更适合长连接的服务,但是长连接的服务Netty比Jetty更优秀

Undertow更适合于IO密集型服务器或者文件服务器,比Tomcat优秀

Jetty(长连接):

<dependency> <artifactId>spring-boot-starter-jetty</artifactId> <groupId>org.springframework.boot</groupId> </dependency>

Undertow(不支持jsp):

<dependency> <artifactId>spring-boot-starter-undertow</artifactId> <groupId>org.springframework.boot</groupId> </dependency> 嵌入式Servlet容器自动配置原理

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

@Configuration @ConditionalOnWebApplication @EnableConfigurationProperties({ServerProperties.class}) public class EmbeddedWebServerFactoryCustomizerAutoConfiguration { public EmbeddedWebServerFactoryCustomizerAutoConfiguration() { } @Configuration @ConditionalOnClass({Tomcat.class, UpgradeProtocol.class}) // 判断当前是否引入了Tomcat依赖 public static class TomcatWebServerFactoryCustomizerConfiguration { public TomcatWebServerFactoryCustomizerConfiguration() { }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsxwgf.html