• SpringBoot autoconfigure


    springboot 实现了根据classpath下依赖内容自动配置bean到IOC容器,----》Auto configuration(自动配
    置).Springboot可以完美配置各种Spring子项目,如Spring mvc,security、data。。。

    springboot auto configuration会尝试推断哪些bean是用户需要的,并进行初始化

    1、通过class标注@configuration,并基于@ConditionalOnClass和OnMissingBean等@Conditional注解的条件过滤,保证springboot满足一定条件下,才会将bean自动注入IOC容器
    2、要使用自动配置,首先要添加@EnableAutoConfiguration注解,springboot通过该注解使用给@import注解完成导入配置的功能,配置在META-INFO/spring.factories中定义

    @Configuration(proxyBeanMethods = false)
    class ServletWebServerFactoryConfiguration {
    
        @Configuration(proxyBeanMethods = false)
        @ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class })
        @ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
        static class EmbeddedTomcat {
    
            @Bean
            TomcatServletWebServerFactory tomcatServletWebServerFactory(
                    ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
                    ObjectProvider<TomcatContextCustomizer> contextCustomizers,
                    ObjectProvider<TomcatProtocolHandlerCustomizer<?>> protocolHandlerCustomizers) {
                TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
                factory.getTomcatConnectorCustomizers()
                        .addAll(connectorCustomizers.orderedStream().collect(Collectors.toList()));
                factory.getTomcatContextCustomizers()
                        .addAll(contextCustomizers.orderedStream().collect(Collectors.toList()));
                factory.getTomcatProtocolHandlerCustomizers()
                        .addAll(protocolHandlerCustomizers.orderedStream().collect(Collectors.toList()));
                return factory;
            }
    
        }
    
        /**
         * Nested configuration if Jetty is being used.
         */
        @Configuration(proxyBeanMethods = false)
        @ConditionalOnClass({ Servlet.class, Server.class, Loader.class, WebAppContext.class })
        @ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
        static class EmbeddedJetty {
    
            @Bean
            JettyServletWebServerFactory JettyServletWebServerFactory(
                    ObjectProvider<JettyServerCustomizer> serverCustomizers) {
                JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
                factory.getServerCustomizers().addAll(serverCustomizers.orderedStream().collect(Collectors.toList()));
                return factory;
            }
    
        }
    
        /**
         * Nested configuration if Undertow is being used.
         */
        @Configuration(proxyBeanMethods = false)
        @ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })
        @ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
        static class EmbeddedUndertow {
    
            @Bean
            UndertowServletWebServerFactory undertowServletWebServerFactory(
                    ObjectProvider<UndertowDeploymentInfoCustomizer> deploymentInfoCustomizers,
                    ObjectProvider<UndertowBuilderCustomizer> builderCustomizers) {
                UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
                factory.getDeploymentInfoCustomizers()
                        .addAll(deploymentInfoCustomizers.orderedStream().collect(Collectors.toList()));
                factory.getBuilderCustomizers().addAll(builderCustomizers.orderedStream().collect(Collectors.toList()));
                return factory;
            }
    
            @Bean
            UndertowServletWebServerFactoryCustomizer undertowServletWebServerFactoryCustomizer(
                    ServerProperties serverProperties) {
                return new UndertowServletWebServerFactoryCustomizer(serverProperties);
            }
    
        }
    
    }
    

    ServletWebServerFactoryAutoConfiguration.java定义:根据Conditional条件注解实现Bean初始化

    @Configuration(proxyBeanMethods = false)
    @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
    @ConditionalOnClass(ServletRequest.class)
    @ConditionalOnWebApplication(type = Type.SERVLET)
    @EnableConfigurationProperties(ServerProperties.class)     // 对应的Properties配置
    @Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
            ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
            ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
            ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
    public class ServletWebServerFactoryAutoConfiguration {
    
        @Bean
        public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(ServerProperties serverProperties,
                ObjectProvider<WebListenerRegistrar> webListenerRegistrars) {
            return new ServletWebServerFactoryCustomizer(serverProperties,
                    webListenerRegistrars.orderedStream().collect(Collectors.toList()));
        }
    
        @Bean
        @ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat")
        public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCustomizer(
                ServerProperties serverProperties) {
            return new TomcatServletWebServerFactoryCustomizer(serverProperties);
        }
    

    Spring整合的有:
    amqp,AOP,CAP,batch,cache,cassandra数据库,codec,condition注入,couchbase,dao,data,elasticsearch,flyway,freemarker,graphql,groovy,gson,h2,hateosa,hazelcast,http,indlux,info,interation,jackson,jdbc,jersey,jms,jmx,joop,jsonb,kafkamldap,lihibase,logging,mail,mongodb,mustache,neo4j,netty,orm,quarts,r2dbc,rsocket,security,sendgrid,session,sql,webervice,websocket,thymealeaf.

    消息队列有:jms,amqp,kafka
    数据库: jdbc,couchbase,r2dbc,mongodb:neo4j
    模板语言:,mustache ,freemarker,thymealeaf
    组件:quarts
    网络协议:http,netty

    参考文档:
    https://www.cnblogs.com/clarino/p/16344670.html

  • 相关阅读:
    如何使用腾讯云web应用防火墙结合API网管提供安全防护?
    Java终止线程的三种方式
    云扩研习社 | RPA高级开发技巧(下)
    物联网-业务数据智能管理应用设计
    单例模式——数据库连接池设计Java代码实现
    rabbitmq发送json格式 utf8编码数据
    人工智能统计学:GPT4、混合效应模型、贝叶斯、Copula、SEM、极值统计学、文献计量学、分位数回归、网络爬虫、近红外光谱
    echarts设置多条折线不是你想的那样简单
    解决 VMware Network Adapter VMnet1 IP 地址冲突导致无法打开路由器管理页面
    2023年天津美术学院专升本报名考试须知
  • 原文地址:https://blog.csdn.net/xiamaocheng/article/details/127094145