The principle of spring boot custom servlet container

How does our custom Servlet container or spring boot custom Servlet container work in spring boot application

First of allServletWebServerFactoryAutoConfiguration In class
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,//Imported to container BeanPostProcessorsRegistrar assembly
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
      BeanDefinitionRegistry registry) {
   if (this.beanFactory == null) {
   registerSyntheticBeanIfMissing(registry, "webServerFactoryCustomizerBeanPostProcessor",
         WebServerFactoryCustomizerBeanPostProcessor.class);//Imported WebServerFactoryCustomizerBeanPostProcessor

//Servlet Configuration operation before initialization of Container Factory
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
     //If you are currently initializing WebServerFactory Components of type
    if (bean instanceof WebServerFactory) {

    return bean;

private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {
   //Get all the customers and call each custom device. customize How to give Servlet Container to assign
    ((Callbacks)LambdaSafe.callbacks(WebServerFactoryCustomizer.class, this.getCustomizers(), webServerFactory, new Object[0]).withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)).invoke((customizer) -> {

private Collection<WebServerFactoryCustomizer<?>> getCustomizers() {
    if (this.customizers == null) {
            //Get all components of this type from the container: WebServerFactoryCustomizer
             //Customized Servlet Container, you can add a WebServerFactoryCustomizer Components of type
        this.customizers = new ArrayList(this.getWebServerFactoryCustomizerBeans());
        this.customizers = Collections.unmodifiableList(this.customizers);

    return this.customizers;

ServerProperties At 2.x It's not a customization, it's a ServletWebServerFactoryCustomizer,And then the customizer passes in ServerProperties To get related configurations



1) Spring boot adds the corresponding WebServerFactory [Tomcat WebServerFactory] to the container according to the import dependency

2) . when the ioc container is started, the postProcessBeforeInitialization method of the postprocessor will be called. When a component in the container wants to create an object (an instance of Tomcat webserverfactory), the postprocessor webserverfactory customizerbeanpostprocessor will be triggered; postProcessBeforeInitialization is called in the applyBeanPropertyValues method in AbstractAutowireCapableBeanFactory

As long as it's an embedded Servlet container factory, the postprocessor works: call the postProcessBeforeInitialization method

3) The postprocessor obtains all the customizations from the container and calls the customization method of the customizations

Tags: Java Spring Tomcat

Posted on Wed, 06 May 2020 12:04:05 -0400 by halojoy