1. 程式人生 > 其它 >8.配置嵌入式Servlet容器

8.配置嵌入式Servlet容器

目錄

8.配置嵌入式Servlet容器

SpringBoot預設使用Tomcat作為嵌入式的Servlet容器;

問題?

1)、如何定製和修改Servlet容器的相關配置;

1、修改和server有關的配置(ServerProperties【也是EmbeddedServletContainerCustomizer】);

修改訪問埠方式一(配置檔案修改):

server.port=8081           // 修改埠號
server.context-path=/crud  // 修改訪問路徑

server.tomcat.uri-encoding=UTF-8    // 修改訪問路徑編碼

//通用的Servlet容器設定
server.xxx
//Tomcat的設定
server.tomcat.xxx

2、編寫一個EmbeddedServletContainerCustomizer:嵌入式的Servlet容器的定製器;來修改Servlet容器的配置

修改訪問埠方式二(配置類中定製):

兩種修改埠號方式底層原理相同,也可以設定訪問路徑

@Bean  //一定要將這個定製器加入到容器中
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
    return new EmbeddedServletContainerCustomizer() {

        //定製嵌入式的Servlet容器相關的規則
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            container.setPort(8083);
        }
    };
}

2)、註冊Servlet三大元件【Servlet、Filter、Listener】

由於SpringBoot預設是以jar包的方式啟動嵌入式的Servlet容器來啟動SpringBoot的web應用,沒有web.xml檔案。

註冊三大元件用以下方式

1.三大元件都可以自定義類實現相應元件的介面,重寫其中介面方法

2.三個元件bean都在擴充套件springmvc配置類中注入容器中,並進行相應配置

ServletRegistrationBean

//註冊三大元件
@Bean
public ServletRegistrationBean myServlet(){
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
    return registrationBean;
}

FilterRegistrationBean

@Bean
public FilterRegistrationBean myFilter(){
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(new MyFilter());
    registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
    return registrationBean;
}

ServletListenerRegistrationBean

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

凡是springmvc中可以設定的元件,都可以在擴充套件springmvc配置類中設定,注入到容器中使用。

SpringBoot幫我們自動配置SpringMVC的時候,自動的註冊SpringMVC的前端控制器:DIspatcherServlet;

全域性配置檔案中可以通過 server.servletPath 來修改SpringMVC前端控制器預設攔截的請求路徑

DispatcherServletAutoConfiguration中:

@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public ServletRegistrationBean dispatcherServletRegistration(
      DispatcherServlet dispatcherServlet) {
   ServletRegistrationBean registration = new ServletRegistrationBean(
         dispatcherServlet, this.serverProperties.getServletMapping());
    //預設攔截: /  所有請求;包靜態資源,但是不攔截jsp請求;   /*會攔截jsp
    //全域性配置檔案中可以通過server.servletPath來修改SpringMVC前端控制器預設攔截的請求路徑
    
   registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
   registration.setLoadOnStartup(
         this.webMvcProperties.getServlet().getLoadOnStartup());
   if (this.multipartConfig != null) {
      registration.setMultipartConfig(this.multipartConfig);
   }
   return registration;
}

2)、SpringBoot能不能支援其他的Servlet容器;

3)、替換為其他嵌入式Servlet容器

預設支援:

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容器自動配置原理;

EmbeddedServletContainerAutoConfiguration:嵌入式的Servlet容器自動配置?

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication
@Import(BeanPostProcessorsRegistrar.class)
//匯入BeanPostProcessorsRegistrar:Spring註解版;給容器中匯入一些元件
//匯入了EmbeddedServletContainerCustomizerBeanPostProcessor:
//後置處理器:bean初始化前後(建立完物件,還沒賦值賦值)執行初始化工作
public class EmbeddedServletContainerAutoConfiguration {
    
        @Configuration
	@ConditionalOnClass({ Servlet.class, Tomcat.class })//判斷當前是否引入了Tomcat依賴;
	@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)//判斷當前容器沒有使用者自己定義EmbeddedServletContainerFactory:嵌入式的Servlet容器工廠;作用:建立嵌入式的Servlet容器
	public static class EmbeddedTomcat {

		@Bean
		public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
			return new TomcatEmbeddedServletContainerFactory();
		}

	}
    
         /**
	 * Nested configuration if Jetty is being used.
	 */
	@Configuration
	@ConditionalOnClass({ Servlet.class, Server.class, Loader.class,
			WebAppContext.class })
	@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
	public static class EmbeddedJetty {

		@Bean
		public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
			return new JettyEmbeddedServletContainerFactory();
		}

	}

	/**
	 * Nested configuration if Undertow is being used.
	 */
	@Configuration
	@ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })
	@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
	public static class EmbeddedUndertow {

		@Bean
		public UndertowEmbeddedServletContainerFactory undertowEmbeddedServletContainerFactory() {
			return new UndertowEmbeddedServletContainerFactory();
		}

	}

1)、EmbeddedServletContainerFactory(嵌入式Servlet容器工廠)

public interface EmbeddedServletContainerFactory {

   //獲取嵌入式的Servlet容器
   EmbeddedServletContainer getEmbeddedServletContainer(
         ServletContextInitializer... initializers);

}

2)、EmbeddedServletContainer:(嵌入式的Servlet容器)

3)、以TomcatEmbeddedServletContainerFactory為例

@Override
public EmbeddedServletContainer getEmbeddedServletContainer(
      ServletContextInitializer... initializers) {
    //建立一個Tomcat
   Tomcat tomcat = new Tomcat();
    
    //配置Tomcat的基本環節
   File baseDir = (this.baseDirectory != null ? this.baseDirectory
         : createTempDir("tomcat"));
   tomcat.setBaseDir(baseDir.getAbsolutePath());
   Connector connector = new Connector(this.protocol);
   tomcat.getService().addConnector(connector);
   customizeConnector(connector);
   tomcat.setConnector(connector);
   tomcat.getHost().setAutoDeploy(false);
   configureEngine(tomcat.getEngine());
   for (Connector additionalConnector : this.additionalTomcatConnectors) {
      tomcat.getService().addConnector(additionalConnector);
   }
   prepareContext(tomcat.getHost(), initializers);
    
   //將配置好的Tomcat傳入進去,返回一個EmbeddedServletContainer;並且啟動Tomcat伺服器
   return getTomcatEmbeddedServletContainer(tomcat);
}

4)、我們對嵌入式容器的配置修改是怎麼生效?

1.修改ServerProperties配置類中屬性
2.EmbeddedServletContainerCustomizer通過servlet容器定製器

EmbeddedServletContainerCustomizer:定製器幫我們修改了Servlet容器的配置?

怎麼修改的原理?

5)、容器中匯入了EmbeddedServletContainerCustomizerBeanPostProcessor

//初始化之前
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    //如果當前初始化的是一個ConfigurableEmbeddedServletContainer型別的元件
    if (bean instanceof ConfigurableEmbeddedServletContainer) {
      postProcessBeforeInitialization((ConfigurableEmbeddedServletContainer) bean);
    }
    return bean;
}

private void postProcessBeforeInitialization(ConfigurableEmbeddedServletContainer bean) {
    //獲取所有的定製器,呼叫每一個定製器的customize方法來給Servlet容器進行屬性賦值;
    for (EmbeddedServletContainerCustomizer customizer : getCustomizers()) {
        customizer.customize(bean);
    }
}

private Collection<EmbeddedServletContainerCustomizer> getCustomizers() {
    if (this.customizers == null) {
        // Look up does not include the parent context
        this.customizers = new ArrayList<EmbeddedServletContainerCustomizer>(
            //從容器中獲取所有這個型別的元件:EmbeddedServletContainerCustomizer
            //定製Servlet容器,給容器中可以新增一個EmbeddedServletContainerCustomizer型別的元件
            this.beanFactory.getBeansOfType(EmbeddedServletContainerCustomizer.class, false, false).values());
        Collections.sort(this.customizers, AnnotationAwareOrderComparator.INSTANCE);
        this.customizers = Collections.unmodifiableList(this.customizers);
    }
    return this.customizers;
}

ServerProperties也是定製器

步驟:

1)、SpringBoot根據匯入的依賴情況,給容器中新增相應的EmbeddedServletContainerFactory【TomcatEmbeddedServletContainerFactory】

2)、容器中某個元件要建立物件就會驚動後置處理器;EmbeddedServletContainerCustomizerBeanPostProcessor;

只要是嵌入式的Servlet容器工廠,後置處理器就工作;

3)、後置處理器,從容器中獲取所有的EmbeddedServletContainerCustomizer,呼叫定製器的定製方法

5、嵌入式Servlet容器啟動原理;

什麼時候建立嵌入式的Servlet容器工廠?什麼時候獲取嵌入式的Servlet容器並啟動Tomcat;

獲取嵌入式的Servlet容器工廠:

1)、SpringBoot應用啟動執行run方法

2)、refreshContext(context);SpringBoot重新整理IOC容器【建立IOC容器物件,並初始化容器,建立容器中的每一個元件】;如果是web應用建立AnnotationConfigEmbeddedWebApplicationContext,否則:AnnotationConfigApplicationContext

3)、refresh(context);重新整理剛才建立好的ioc容器;

public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      // Prepare this context for refreshing.
      prepareRefresh();

      // Tell the subclass to refresh the internal bean factory.
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      // Prepare the bean factory for use in this context.
      prepareBeanFactory(beanFactory);

      try {
         // Allows post-processing of the bean factory in context subclasses.
         postProcessBeanFactory(beanFactory);

         // Invoke factory processors registered as beans in the context.
         invokeBeanFactoryPostProcessors(beanFactory);

         // Register bean processors that intercept bean creation.
         registerBeanPostProcessors(beanFactory);

         // Initialize message source for this context.
         initMessageSource();

         // Initialize event multicaster for this context.
         initApplicationEventMulticaster();

         // Initialize other special beans in specific context subclasses.
         onRefresh();

         // Check for listener beans and register them.
         registerListeners();

         // Instantiate all remaining (non-lazy-init) singletons.
         finishBeanFactoryInitialization(beanFactory);

         // Last step: publish corresponding event.
         finishRefresh();
      }

      catch (BeansException ex) {
         if (logger.isWarnEnabled()) {
            logger.warn("Exception encountered during context initialization - " +
                  "cancelling refresh attempt: " + ex);
         }

         // Destroy already created singletons to avoid dangling resources.
         destroyBeans();

         // Reset 'active' flag.
         cancelRefresh(ex);

         // Propagate exception to caller.
         throw ex;
      }

      finally {
         // Reset common introspection caches in Spring's core, since we
         // might not ever need metadata for singleton beans anymore...
         resetCommonCaches();
      }
   }
}

4)、onRefresh(); web的ioc容器重寫了onRefresh方法

5)、webioc容器會建立嵌入式的Servlet容器;createEmbeddedServletContainer();

6)、獲取嵌入式的Servlet容器工廠:

EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();

從ioc容器中獲取EmbeddedServletContainerFactory 元件;TomcatEmbeddedServletContainerFactory建立物件,後置處理器一看是這個物件,就獲取所有的定製器來先定製Servlet容器的相關配置;

7)、使用容器工廠獲取嵌入式的Servlet容器:this.embeddedServletContainer = containerFactory.getEmbeddedServletContainer(getSelfInitializer());

8)、嵌入式的Servlet容器建立物件並啟動Servlet容器;

先啟動嵌入式的Servlet容器,再將ioc容器中剩下沒有創建出的物件獲取出來;

IOC容器啟動建立嵌入式的Servlet容器