SpringBoot2.x built to automatically configure Tomcat principle

 SpringBoot embedded tomcat how autoconfiguration?

  How we modify the default parameters of the embedded tomcat?

With these two questions, we analyzed under SpringBoot built-in automatic configuration tomcat principle

We know SpringBoot automatic configuration are in the spring-boot-autoconfigure-xx.RELEASE.jar package.

Meanwhile SpringBoot of supported servlet web server implementations do modeling abstract:

 

Servlet container type  WebServer Model Interface WebServer factory implementation class
Tomcat    TomcatWebServer     TomcatServletWebServerFactory
Jetty    JettyWebServer    JettyServletWebServerFactory
Undertow  UndertowWebServer  UndertowServletWebServerFactory

Based on this concept model, in a Web application types, Springboot uses said web to form the corresponding plant components webserver instance. The web components come from the factory, which is automatically configured class ServletWebServerFactoryAutoConfiguaration task automatically generate the web configuration of the plant components.

    

@Configuration
@AutoConfigureOrder(-2147483648)
@ConditionalOnClass({ServletRequest.class})
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@EnableConfigurationProperties({ServerProperties.class})
@Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class})
public class ServletWebServerFactoryAutoConfiguration {
    public ServletWebServerFactoryAutoConfiguration() {
    }

    @Bean
    public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(ServerProperties serverProperties) {
        return new ServletWebServerFactoryCustomizer(serverProperties);
    }

    @Bean
    @ConditionalOnClass(
        name = {"org.apache.catalina.startup.Tomcat"}
    )
    public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCustomizer(ServerProperties serverProperties) {
        return new TomcatServletWebServerFactoryCustomizer(serverProperties);
    }

    public static class BeanPostProcessorsRegistrar implements ImportBeanDefinitionRegistrar, BeanFactoryAware {
        private ConfigurableListableBeanFactory beanFactory;

        public BeanPostProcessorsRegistrar() {
        }

        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
            if (beanFactory instanceof ConfigurableListableBeanFactory) {
                this.beanFactory = (ConfigurableListableBeanFactory)beanFactory;
            }

        }

        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
            if (this.beanFactory != null) {
                this.registerSyntheticBeanIfMissing(registry, "webServerFactoryCustomizerBeanPostProcessor", WebServerFactoryCustomizerBeanPostProcessor.class);
                this.registerSyntheticBeanIfMissing(registry, "errorPageRegistrarBeanPostProcessor", ErrorPageRegistrarBeanPostProcessor.class);
            }
        }

        private void registerSyntheticBeanIfMissing(BeanDefinitionRegistry registry, String name, Class<?> beanClass) {
            if (ObjectUtils.isEmpty(this.beanFactory.getBeanNamesForType(beanClass, true, false))) {
                RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass);
                beanDefinition.setSynthetic(true);
                registry.registerBeanDefinition(name, beanDefinition);
            }

        }
    }
}

 First look at this ServletWebServerFactoryAutoConfiguration automatically configures the class a few notes:

Condition two indicates that the current operating environment is based on web servlet service standards: ConditionalOnClass (ServletRequest.class): Indicates the current must exist ConditionalOnWebApplication (type = Type.SERVLET) servlet-api-dependent: only servlet-based Web application

 

1. @AutoConfigureOrder This loading sequence is determined annotation configuration class, a smaller value when the annotation in the first loading

2. @ EnableConfigurationProperties open configuration attribute value ServerProperties class. And this is the class which contains webserver configuration

3. @Import which internal class BeanPostProcessorsRegistrar, this category registered two bean:

    WebServerFactoryCustomizerBeanPostProcessor and ErrorPageRegistarBeanPostProcess. The role of both the bean and then more on this later

4. @ Import EmbeddedTomcat class

   

 @Configuration
    @ConditionalOnClass({Servlet.class, Tomcat.class, UpgradeProtocol.class})
    @ConditionalOnMissingBean(
        value = {ServletWebServerFactory.class},
        search = SearchStrategy.CURRENT
    )
    public static class EmbeddedTomcat {
        public EmbeddedTomcat() {
        }

        @Bean
        public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
            return new TomcatServletWebServerFactory();
        }
    }

  This class will be added in the presence of a TomcatServletWebServerFactory bean tomcat related jar package  

5. In addition, a further injection

ServletWebServerFactoryCustomizer和TomcatServletWebServerFactoryCustomizer




 

Guess you like

Origin www.cnblogs.com/whx7762/p/12302983.html