SpringBoot源码深度解析

Spring开源框架,解决企业级开发的复杂性的问题,简化开发AOP, IOCSpring 配置越来多,配置不方便管理!

Javaweb---Servlet+tomcat + Struct2  SpringMVC  SPRINGboot.....

所有的技术框架:从一个复杂的场景慢慢的衍生出来一种规范!简单的配置!

==SpringBoot :自动配置!==

Springboot怎么自动配置,核心原理!当之无愧的Java领域最热门的技术!公司,怎么演进架构!

1、 版本控制

资源控制


        org.springframework.boot
        spring-boot-starter-parent
        2.2.1.RELEASE
         
    

资源控制


    org.springframework.boot
    spring-boot-dependencies
    2.2.1.RELEASE
    ../../spring-boot-dependencies
  

  
    5.15.10
    2.7.7
    1.9.76
    2.10.1
    1.9.4
    3.13.2
    4.0.6
    4.0.1
    2.1.4
    3.0.0
    1.10.2
    2.8.0
    3.7.2
    1.5.1
    1.13
    2.7.0
    3.9
    1.6
    2.7.0
    2.1.0
    2.7.9
    11.5.0.0
    1.0.8.RELEASE
    10.14.2.0
    4.1.1
    2.10.6
    3.8.1
    6.8.4
    2.2.0
    1.6.0
    1.1.0
    6.0.8
    2.3.29
    3.0.1
    3.0.3
    2.3.2
    2.5.8
    2.8.6
    1.4.200
    2.1
    3.12.4
    1.3.2
    5.4.8.Final
    6.0.18.Final
    3.4.1
    2.5.0
    2.36.0
    4.1.4
    4.5.10
    4.4.12
    9.4.16.Final
    2.15
    2.10.0
    ${jackson.version}
    1.2.1
    1.3.5
    2.0.3
    1.1.6
    1.0.2
    1.6.4
    2.2.3
    4.0.3
    2.3.5
    1.2.7
    1.3.3
    2.0.1
    1.1.2
    2.1.6
    2.3.2
    2.3.2
    3.1.0
    1.2.0
    1.3.2
    1.1.1
    2.3.1
    2.3.1
    2.0.1
    1.1.4
    1.0
    1.6.2
    1.0.3
    2.2
    1.3
    2.0.1.Final
    1.1
    1.2.0
    3.0.6
    3.4.1.Final
    7.6.0.Final
    2.0.6
    3.1.0
    2.29.1
    6.3.1
    9.4.22.v20191022
    8.5.40
    2.2.0.v201112011158
    1.0.3
    1.15
    4.5.2
    2.10.5
    ${johnzon-jsonb.version}
    1.2.1
    1.6.2
    3.12.3
    1.5.0
    2.4.0
    1.2
    1.3.1
    4.12
    5.5.2
    2.3.1
    1.3.50
    1.3.2
    5.2.1.RELEASE
    3.8.0
    2.12.1
    1.2.3
    1.18.10
    2.4.4
    1.8
    3.1.1
    3.1.0
    3.8.1
    3.1.1
    2.8.2
    3.0.0-M2
    2.22.2
    3.2.0
    2.5.2
    3.2.1
    3.1.2
    3.1.1
    3.1.0
    3.2.1
    3.8.2
    3.1.0
    2.22.2
    3.2.3
    1.3.1
    1.9.12
    3.1.0
    3.11.2
    1.12.0
    7.4.1.jre8
    8.0.18
    1.9.22
    3.2.2
    4.1.43.Final
    2.0.26.Final
    1.1.0
    19.3.0.0
    3.14.4
    1.0.6
    42.2.8
    0.7.0
    2.3.2
    4.2.1
    5.7.3
    1.0.3
    Dysprosium-SR1
    3.3.0
    1.0.0-RC5
    1.3.8
    2.2.14
    1.2.1
    1.5.1
    3.141.59
    2.36.0
    4.4.1
    4.0.1
    1.7.29
    1.25
    8.2.0
    2.2.1.RELEASE
    4.2.0.RELEASE
    2.0.6.RELEASE
    Moore-SR1
    5.2.1.RELEASE
    1.0.1.RELEASE
    5.2.1.RELEASE
    2.3.3.RELEASE
    2.3.2.RELEASE
    2.0.4.RELEASE
    1.2.4.RELEASE
    5.2.1.RELEASE
    Corn-RELEASE
    3.0.8.RELEASE
    3.28.0
    ${jakarta-mail.version}
    3.0.11.RELEASE
    2.0.1
    3.0.4.RELEASE
    3.0.4.RELEASE
    2.4.1
    9.0.27
    4.0.12
    2.0.27.Final
    2.7
    3325375
    0.41
    1.6.3
    1.0.2
    2.6.3
  

pom.xml 就是springboot的版本控制中心!

假设里面有默认的配置,我们就不需手动配置版本了,假设没有,我们才需要手动配置!

2、 启动器

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//程序的主入口
@SpringBootApplication  //Springboot应用
public class DemoApplication {

    public static void main(String[] args) {
        //开启了一个服务!
        SpringApplication.run(DemoApplication.class, args);
    }

}

applicationContext.xml  == @Configuration

 bean                                ==  @Bean

import                              == @Import

所有的包,必须在主启动类的同级或子级目录!为什么?

protected List getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {

    List configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
                                                                         getBeanClassLoader());
    Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
                    + "are using a custom packaging, make sure that file is correct.");
    return configurations;
}

 

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveRestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.rsocket.RSocketSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration,\
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateA

复制代码

在springboot已启动,它就自动帮我们配置了以上全部的类!但是真的都生效的了?

WebMvcAutoConfiguration.xml
  bean

怎么自动配置的?

3、SpringApplication

  1. 推断我们的应用是web应用还是 普通应用!

  2. 加载初始化!

  3. 设置监听器

  4. 推断main方法,找到运行的主类

run方法()核心!

4、YAML

但为了强调这种语言以数据做为中心

字面量键值对:

k: (空格) value

对象,map

 

# 语法
k:
 v1:
 v2:
 
# 例子
student:
  name: coding
  age: 3
 
student: {name: coding, age: 3}

 

数组(List,Set)

#语法
pets:
 - cat
 - dog
 - pig
 
pets: [cat,dog,pig]

 @ConfigurationProperties :SpringBoot自动配置底层全部都是这个注解

 SpringBoot源码深度解析_第1张图片

 

 

精髓

@ConditionalOnXXX根据这个类中不同的条件判断这个类是否生效!

  1. 一旦这个配置类生效,这个配置类就会往容器中添加各种组件!

  2. 这些个组件,都可以和xxxProerties绑定,而 xxProerties  @ConfigurationProperties  和我们的配置文件绑定,所以说,我们只需要少量配置就可以实现功能了,假设不配置,就会使用springboot 的默认配置

  3. Springboot中存在大量自动动配置类

    但是这些类不一定生效! @ConditionalOnXXX

  4. xxxAutoConfiguration,自动配置类

  5. xxxProperties,配置类 @ConfigurationProperties  组合

  6. 实现和我们的配置一一对应,我们假设没有配置就使用自动配置,如果配置了,就使用我们自动配置的!

架构演进推演

聊聊过去和未来!

三层架构  mvc
  架构---> 结构
  
开发框架
  Spring
    IOC:控制反转思想  DI
          约泡:
            泡温泉,泡茶,。。。。泡友
            附近的人,打招呼,加微信,聊聊天天聊,,,,,约泡   手动实现,一步步new
            
            浴场: 温泉,茶庄的... 容器(get 拿)
              直接有泡友了!
                
      原来需要一步步自己去操作,现在交个容器了,我们需要什么区拿就可以了!
      
    AOP: 面向切面 (动态代理)
      不影响原来业务的情况下,动态增加功能,日志,事务,安全///.
  
  解决企业开发的复杂性
  Spring 春天,简化了开发,随着时间的推移,开始慢慢的复杂起来,配置文件变多!
  
  SpringBoot
    SpringBoot 并不是新的东西,就是Spring升级版!
    新一代JavaEE开发标准,开箱即用!
    自动配置=====核心原理!
    约定大于配置
    
单体架构  all in one

微服务架构---新架构
  模块化,功能化!
  代码没有变化,网络问题
  
  
  
微服务架构的问题? 4步曲
  1. 这么多服务,客户端如何去访问?
  2. 这么多服务,服务之间如何通信?
  3. 这么多服务,怎么治理?
  4. 这么多服务,服务挂了怎么办?
  
  
SpringCloud, 生态.,

解决方案:
  1. Spring Cloud Netflix  一站式解决方案
    api:----zuul
    Feign---》 HttpClient =-- Http 的同学方式,同步并阻塞
    服务注册与发现====》Eureka
    熔断机制====》Hystrix
    
    Netflix 2018,宣布无限期停止维护!
    
    2. Apache Dubbo + Zookeeper  不完善!
      API;没有  借助别人的或者自己写
      Dubbo:异步非阻塞 RPC
      服务注册与发现: Zookeeper (Hadoop,Hive。。。)
      熔断机制没有。借助  Hystrix
  
  3. Spring Cloud Alibaba 一站式解决方案!
  
  
目前又推出新一代的服务网格概念
   下一代的服务标准: Server Mesh  服务网格
   Istio:未来需要掌握
   
   1   1.8  裁员对于优秀的人来说是好事!
   
万变不离其宗!一通百通!
  1. 这么多服务,客户端如何去访问?
  2. 这么多服务,服务之间如何通信?
  3. 这么多服务,怎么治理?
  4. 这么多服务,服务挂了怎么办?
  
  如果你们机遇这些问题,开发出来一套解决方案!  投资自己
  
为什么要解决这问题: 本质: 网络是不可靠的!

高级程序猿 


程序猿---项目经理---架构师----CTO---CEO

架构:设计出来,衍生出来的!
架是一个逐步演进的过程!架构没有最好的,只有最合适!


-----------------------
当下进阶的一个任务!
1、 定义当前企业的架构,所处的阶段,绘制出一个架构图(完成)
2、 清晰定位当前公司的一个状况!根据企业的实际情况绘制出新的架构图(重构!)

分布式
高可用
集群
负载均衡
.....

如果需要本期图文教程的视频教程,请关注“艾编程”公众号,回复关键词“源码解析”或者“SpringBoot源码”,看本期课程的视频+图文教程,还能收获更多哦! 

你可能感兴趣的:(架构师,爱编程)