SpringBoot整合Dubbo

简介:前端

  1. Spring Boot是由Pivotal团队提供的, Spring家族中的一个全新的框架,它用来简化Spring应用程序的建立和开发过程,也能够说Spring boot能简化咱们以前采用SpringMVC+Spring+Mybatis框架进行开发的过程。
  2. 在以往咱们采用SpringMVC+Spring+Mybatis框架进行开发的时候,搭建和整合三大框架,咱们须要作很好工做,好比配置web.xml,配置Spring,配置Mybatis,并将它们整合在一块儿等,而Spring boot框架对此开发过程进行了革命性的颠覆,抛弃了繁琐的xml配置过程,采用大量的默认配置简化咱们的开发过程.
  3. 因此采用Spring boot能够很是容易和快速的建立基于Spring框架的应用程序,它让编码变简单了,配置变简单了,部署变简单了,监控也变简单了。

1、SpringBoot特征:java

  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • Provide opinionated 'starter' dependencies to simplify your build configuration
  • Automatically configure Spring and 3rd party libraries whenever possible
  • Provide production-ready features such as metrics, health checks and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration

2、SpringBoot版本:web

CURRENT: 当前最新版本redis

GA:General Availability,正式发布的版本,官方推荐使用此版本。在国外都是用GA来讲明release版本的。spring

PRE: 预览版,内部测试版. 主要是给开发人员和测试人员测试和找BUG用的,不建议使用;apache

SNAPSHOT: 快照版,能够稳定使用,且仍在继续改进版本。api

3、SpringBoot application starters:数组

Springboot提供了一些主流的整合包以下,可是一些非主流的整合就由整合方本身提供,例如springboot和dubbo的整合是由apache提供整合包。springboot

4、Dubbo背景:markdown

单一应用架构

当网站流量很小时,只需一个应用,将全部功能都部署在一块儿,以减小部署节点和成本。此时,用于简化增删改查工做量的数据访问框架(ORM)是关键。

垂直应用架构

当访问量逐渐增大,单一应用增长机器带来的加速度愈来愈小,将应用拆成互不相干的几个应用,以提高效率。此时,用于加速前端页面开发的Web框架(MVC)是关键。

分布式服务架构

当垂直应用愈来愈多,应用之间交互不可避免,将核心业务抽取出来,做为独立的服务,逐渐造成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。此时,用于提升业务复用及整合的分布式服务框架(RPC)是关键。

流动计算架构

当服务愈来愈多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增长一个调度中心基于访问压力实时管理集群容量,提升集群利用率。此时,用于提升机器利用率的资源调度和治理中心(SOA)是关键。

5、SpringBoot整合Dubbo:

参考文档1:com.alibaba

参考文档2:apache dubbo

SpringBoot整合dubbo版本要求:

Dubbo Spring Boot Dubbo Spring Boot 0.2.1.RELEASE 2.6.5+ 2.x
0.1.2.RELEASE 2.6.5+ 1.x

步骤一:建立一个项目boot-dubbo,在父模块pom.xml引入基本配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.clinks</groupId>
    <artifactId>boot-dubbo-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 在这里设置打包类型为pom,做用是为了实现多模块项目 -->
    <packaging>pom</packaging>


    <!-- 第一步:添加Springboot的parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.9.RELEASE</version>
    </parent>

    <modules>
        <module>../boot-dubbo-dl</module>
        <module>../boot-dubbo-service</module>
      <module>../boot-dubbo-web</module>
    </modules>

    <!-- 版本属性 -->
    <properties>
        <!--springBoot整合dubbo依赖-->
        <dubbo.boot.version>0.2.1.RELEASE</dubbo.boot.version>
        <dubbo.version>2.6.5</dubbo.version>
    </properties>

</project>
复制代码

步骤二:建立三个子模块:boot-dubbo-api、boot-dubbo-provider、boot-dubbo-web

boot-dubbo-api做为接口模块,打成jar包可被另外两个模块引用,全部的服务接口声明均可写在该模块;

boot-dubbo-provider做为服务提供方,接口实现类可写在该模块,并被注册到注册中心,可供其余模块调用;

boot-dubbo-web做为服务消费方,能够rpc远程调用服务。

步骤三:boot-dubbo-dl部分pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <artifactId>boot-dubbo-dl</artifactId>
    <packaging>jar</packaging>

    <parent>
        <artifactId>boot-dubbo-parent</artifactId>
        <groupId>com.clinks</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../boot-dubbo-parent/pom.xml</relativePath>
    </parent>

    <dependencies>
        <!-- jar依赖 -->
        <dependency>
            <groupId>com.clinks</groupId>
            <artifactId>boot-dubbo-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- 核心SpringBoot-starter依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- springBoot-dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>

        <!--使用redis作注册中心-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>
</project>
复制代码

步骤4、配置文件

----xml方式----
<dubbo:application name="agent-hr-dl"/>
<dubbo:registry address="${dubbo.address}" username="test" password="${redis.password}"/>
<dubbo:consumer retries="0"  filter="cat_filter"/>
<dubbo:service interface="com.clinks.agent.hr.service.IAgentKpiService" ref="agentKpiServiceImpl"/>

----注解方式----
dubbo.application.name=dubbo-consumer-annotation
dubbo.registry.address=redis://139.196.28.237:6641
dubbo.registry.username=test
dubbo.registry.password=Toodc600
dubbo.consumer.timeout=3000
dubbo.consumer.registries=0

---javaConfig方式
复制代码

步骤五:核心启动类Application

@SpringBootApplication
@EnableDubbo
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}
复制代码

步骤六:boot-dubbo-web部分pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.clinks</groupId>
        <artifactId>boot-dubbo-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../boot-dubbo-parent</relativePath>
    </parent>

    <groupId>com.clinks</groupId>
    <artifactId>boot-dubbo-web</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- jar依赖 -->
        <dependency>
            <groupId>com.clinks</groupId>
            <artifactId>boot-dubbo-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- 核心SpringBoot-starter依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- springBoot-dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!--web模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--test模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <!--redis-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>
</project>
复制代码

6、核心注解篇

注解一:@SpringBootApplication:**

组合注解:

@Configuration :是一个类注解,标明这是一个配置类,将启动类声明成一个配置类

通常与@Bean合用,做用至关于

@ComponentScan :指定扫描包

Springboot默认扫描同包以及子包下注解,如@Controller,@Service,@Configuration

@EnableAutoConfiguration : 开启 Spring 应用程序上下文的自动配置

原理简单说明:

注解二:@EnableDubbo

  1. @EnableDubbo 注解是 @EnableDubboConfig 和 @DubboComponentScan二者组合的便捷表达方式。与注解驱动相关的是 @DubboComponentScan。
  2. 经过 @EnableDubbo 能够在指定的包名下(经过 scanBasePackages),或者指定的类中(经过 scanBasePackageClasses)扫描 Dubbo 的服务提供者(以 @Service 标注)以及 Dubbo 的服务消费者(以 Reference 标注)。
  3. 扫描到 Dubbo 的服务提供方和消费者以后,对其作相应的组装并初始化,并最终完成服务暴露或者引用的工做。

注解3、@Service

经过 @Service 上提供的属性,能够进一步的定制化 Dubbo 的服务提供方:

package org.apache.dubbo.config.annotation;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE}) // #1
@Inherited
public @interface Service {
    Class<?> interfaceClass() default void.class; // #2
    String interfaceName() default ""; // #3
    String version() default ""; // #4
    String group() default ""; // #5
    boolean export() default true; // #6
    boolean register() default true; // #7
    
    String application() default ""; // #8
    String module() default ""; // #9
    String provider() default ""; // #10
    String[] protocol() default {}; // #11
    String monitor() default ""; // #12
    String[] registry() default {}; // #13
}
复制代码

其中比较重要的有:

  1. @Service 只能定义在一个类上,表示一个服务的具体实现
  2. interfaceClass:指定服务提供方实现的 interface 的类
  3. interfaceName:指定服务提供方实现的 interface 的类名
  4. version:指定服务的版本号
  5. group:指定服务的分组
  6. export:是否暴露服务
  7. registry:是否向注册中心注册服务
  8. application:应用配置
  9. module:模块配置
  10. provider:服务提供方配置
  11. protocol:协议配置
  12. monitor:监控中心配置
  13. registry:注册中心配置

注解4、@Reference

经过 @Reference 上提供的属性,能够进一步的定制化 Dubbo 的服务消费方:

package org.apache.dubbo.config.annotation;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) // #1
public @interface Reference {
    Class<?> interfaceClass() default void.class; // #2
    String interfaceName() default ""; // #3
    String version() default ""; // #4
    String group() default ""; // #5
    String url() default ""; // #6
    
    String application() default ""; // #7
    String module() default ""; // #8
    String consumer() default ""; // #9
    String protocol() default ""; // #10
    String monitor() default ""; // #11
    String[] registry() default {}; // #12
}
复制代码

其中比较重要的有:

  1. @Reference 能够定义在类中的一个字段上,也能够定义在一个方法上,甚至能够用来修饰另外一个 annotation,表示一个服务的引用。一般 @Reference 定义在一个字段上
  2. interfaceClass:指定服务的 interface 的类
  3. interfaceName:指定服务的 interface 的类名
  4. version:指定服务的版本号
  5. group:指定服务的分组
  6. url:经过指定服务提供方的 URL 地址直接绕过注册中心发起调用
  7. application:应用配置
  8. module:模块配置
  9. consumer:服务消费方配置
  10. protocol:协议配置
  11. monitor:监控中心配置
  12. registry:注册中心配置

7、SpringBoot源码解析

启动流程:

一、建立SpringApplication对象

initialize(sources);
private void initialize(Object[] sources) {    
  //保存主配置类     
  if (sources != null && sources.length > 0) {         this.sources.addAll(Arrays.asList(sources));     
  } 
  
  //判断当前是否一个web应用     
  this.webEnvironment = deduceWebEnvironment(); 
  
  //从类路径下找到META‐INF/spring.factories配置的全部ApplicationContextInitializer;而后保存起来 
  setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class);     
  //从类路径下找到ETA‐INF/spring.factories配置的全部ApplicationListener
  setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));   
                  
  //从多个配置类中找到有main方法的主配置类
  this.mainApplicationClass = deduceMainApplicationClass(); 
 }
复制代码

二、运行run()方法

public ConfigurableApplicationContext run(String... args) {    StopWatch stopWatch = new StopWatch();    
   stopWatch.start();
   ConfigurableApplicationContext context = null;    
   FailureAnalyzers analyzers = null;    
   configureHeadlessProperty(); 
                                                           
   //获取SpringApplicationRunListeners;从类路径下META‐INF/spring.factories
                                                                  		SpringApplicationRunListeners listeners = getRunListeners(args);
                                                                      
    //回调全部的获取SpringApplicationRunListener.starting()方法    
    listeners.starting();   
     try {       
       //封装命令行参数
       ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);       
       
       //准备环境       
       ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments); 
       //建立环境完成后回调SpringApplicationRunListener.environmentPrepared();表示环境准备完成
       
       Banner printedBanner = printBanner(environment);                
       
       //建立ApplicationContext;决定建立web的ioc仍是普通的ioc 
       context = createApplicationContext();               analyzers = new FailureAnalyzers(context);        
       //准备上下文环境;将environment保存到ioc中;并且applyInitializers();        //applyInitializers():回调以前保存的全部的ApplicationContextInitializer的initialize方法        //回调全部的SpringApplicationRunListener的contextPrepared();             
      
       prepareContext(context, environment, listeners, applicationArguments, printedBanner);        
       //prepareContext运行完成之后回调全部的SpringApplicationRunListener的contextLoaded();        
       //刷新容器;ioc容器初始化(若是是web应用还会建立嵌入式的Tomcat);Spring注解版       
       //扫描,建立,加载全部组件的地方;(配置类,组件,自动配置)      
       refreshContext(context);       
       
       //从ioc容器中获取全部的ApplicationRunner和CommandLineRunner进行回调        //ApplicationRunner先回调,CommandLineRunner再回调 
       afterRefresh(context, applicationArguments);  
       
       //全部的SpringApplicationRunListener回调finished方法 
       listeners.finished(context, null);       
       stopWatch.stop();       
       if (this.logStartupInfo) {         
         new StartupInfoLogger(this.mainApplicationClass)                .logStarted(getApplicationLog(), stopWatch);      
       }        
       //整个SpringBoot应用启动完成之后返回启动的ioc容器;       
       return context;    
     }   catch (Throwable ex) {      
       handleRunFailure(context, listeners, analyzers, ex); 
       throw new IllegalStateException(ex);    
     }
}
复制代码

3. 生命周期流程:

// 使用SpringFactoriesLoader在应用的classpath中查找并加载全部可用的ApplicationContextInitializer。
// 使用SpringFactoriesLoader在应用的classpath中查找并加载全部可用的ApplicationListener。     
    初始化SpringApplication时候收集各类回调接口

// SpringFactoriesLoader能够查找到并加载的SpringApplicationRunListener
SpringApplicationRunListeners listeners = this.getRunListeners(args);

// 说明SpringBoot开始执行了
listeners.starting();  

// 建立而且配置SpringBoot使用环境
this.prepareEnvironment(listeners );  {  listeners.environmentPrepared();  // 说明SpringBoot环境已经配置好  }

// 建立Spring容器
context = this.createApplicationContext()

// 配置Spring容器上下文
 this.prepareContext(context, environment, listeners, applicationArguments, printedBanner); {
       
        this.applyInitializers(context); {  this.getInitializers() .initialize(context)  }
        listeners.contextPrepared(context);  {  this.listeners(). contextPrepared(context)   // 在建立和准备好ApplicationContext以后,但在加载源以前调用。 }
        listeners.contextLoaded(context);  {   this.listeners().contextLoaded(context)  //在加载应用程序上下文后但刷新以前调用 }

}

// 刷新Spring容器,经过反射生成bean
this.refreshContext(context);

this.afterRefresh(); 

//上下文已刷新,应用程序已启动,但还没有调用commandlinerunner和applicationrunner
listeners.started(context);

// 查找当前ApplicationContext中是否注册有CommandLineRunner,若是有,则遍历执行它们。
this.callRunners(context, applicationArguments);  {  ApplicationRunner;   CommandLineRunner;   }
复制代码

8、SpringBoot自动配置

SpringBoot为咱们作的自动配置,确实方便快捷,可是对于新手来讲,若是不大懂SpringBoot内部启动原理,之后不免会吃亏。

入口类

咱们开发任何一个Spring Boot项目,都会用到以下的启动类

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
复制代码

从上面代码能够看出,Annotation定义(@SpringBootApplication)和类定义(SpringApplication.run)最为耀眼,因此要揭开SpringBoot的神秘面纱,咱们要从这两位开始就能够了。

SpringBootApplication背后的秘密

@Target(ElementType.TYPE)            // 注解的适用范围,其中TYPE用于描述类、接口(包括包注解类型)或enum声明
@Retention(RetentionPolicy.RUNTIME)  // 注解的生命周期,保留到class文件中(三个生命周期)
@Documented                          // 代表这个注解应该被javadoc记录
@Inherited                           // 子类能够继承该注解
@SpringBootConfiguration             // 继承了Configuration,表示当前是注解类
@EnableAutoConfiguration             // 开启springboot的注解功能,springboot的四大神器之一,其借助@import的帮助
@ComponentScan(excludeFilters = {    // 扫描路径设置(具体使用待确认)
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
...
}
复制代码

虽然定义使用了多个Annotation进行了原信息标注,但实际上重要的只有三个Annotation:

  • @Configuration(@SpringBootConfiguration点开查看发现里面仍是应用了@Configuration)
  • @EnableAutoConfiguration
  • @ComponentScan

因此,若是咱们使用以下的SpringBoot启动类,整个SpringBoot应用依然能够与以前的启动类功能对等:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
复制代码

每次写这3个比较累,因此写一个@SpringBootApplication方便点。接下来分别介绍这3个Annotation。

@Configuration

这里的@Configuration对咱们来讲不陌生,它就是JavaConfig形式的Spring Ioc容器的配置类使用的那个@Configuration,SpringBoot社区推荐使用基于JavaConfig的配置形式,因此,这里的启动类标注了@Configuration以后,自己其实也是一个IoC容器的配置类。

举几个简单例子回顾下,XML跟config配置方式的区别:

    • 表达形式层面 基于XML配置的方式是这样:

而基于JavaConfig的配置方式是这样:

@Configuration
public class MockConfiguration{
    //bean定义
}
复制代码

任何一个标注了@Configuration的Java类定义都是一个JavaConfig配置类。

    • 注册bean定义层面 基于XML的配置形式是这样:

      ...

而基于JavaConfig的配置形式是这样的:

@Configuration
public class MockConfiguration{
    @Bean
    public MockService mockService(){
        return new MockServiceImpl();
    }
}
复制代码

任何一个标注了@Bean的方法,其返回值将做为一个bean定义注册到Spring的IoC容器,方法名将默认成该bean定义的id。

    • 表达依赖注入关系层面 为了表达bean与bean之间的依赖关系,在XML形式中通常是这样:

而基于JavaConfig的配置形式是这样的:

@Configuration
public class MockConfiguration{
    @Bean
    public MockService mockService(){
        return new MockServiceImpl(dependencyService());
    }
    
    @Bean
    public DependencyService dependencyService(){
        return new DependencyServiceImpl();
    }java
}
复制代码

若是一个bean的定义依赖其余bean,则直接调用对应的JavaConfig类中依赖bean的建立方法就能够了。

@ComponentScan

@ComponentScan这个注解在Spring中很重要,它对应XML配置中的元素,@ComponentScan的功能其实就是自动扫描并加载符合条件的组件(好比@Component和@Repository等)或者bean定义,最终将这些bean定义加载到IoC容器中。

咱们能够经过basePackages等属性来细粒度的定制@ComponentScan自动扫描的范围,若是不指定,则默认Spring框架实现会从声明@ComponentScan所在类的package进行扫描。

@EnableAutoConfiguration

我的感受@EnableAutoConfiguration这个Annotation最为重要,因此放在最后来解读,你们是否还记得Spring框架提供的各类名字为@Enable开头的Annotation定义?好比@EnableScheduling、@EnableCaching、@EnableMBeanExport等,@EnableAutoConfiguration的理念和作事方式其实一脉相承,简单归纳一下就是,借助@Import的支持,收集和注册特定场景相关的bean定义。

  • @EnableScheduling是经过@Import将Spring调度框架相关的bean定义都加载到IoC容器。
  • @EnableMBeanExport是经过@Import将JMX相关的bean定义加载到IoC容器。

而@EnableAutoConfiguration也是借助@Import的帮助,将全部符合自动配置条件的bean定义加载到IoC容器,仅此而已!

@EnableAutoConfiguration做为一个复合Annotation,其自身定义关键信息以下:

@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    ...
}
复制代码

两个比较重要的注解:

  • @AutoConfigurationPackage:自动配置包
  • @Import: 导入自动配置的组件

AutoConfigurationPackage注解:

1     static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
2 
3         @Override
4         public void registerBeanDefinitions(AnnotationMetadata metadata,
5                 BeanDefinitionRegistry registry) {
6             register(registry, new PackageImport(metadata).getPackageName());
7         }
复制代码

它实际上是注册了一个Bean的定义。

new PackageImport(metadata).getPackageName(),它其实返回了当前主程序类的 *同级以及子级 * 的包组件。

以上图为例,DemoApplication是和demo包同级,可是demo2这个类是DemoApplication的父级,和example包同级

也就是说,DemoApplication启动加载的Bean中,并不会加载demo2,这也就是为何,咱们要把DemoApplication放在项目的最高级中。

Import(AutoConfigurationImportSelector.class)注解:

能够从图中看出 ** AutoConfigurationImportSelector 继承了 DeferredImportSelector 继承了 ImportSelector**

ImportSelector有一个方法为:selectImports。

@Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        }
        AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
                .loadMetadata(this.beanClassLoader);
        AnnotationAttributes attributes = getAttributes(annotationMetadata);
        List<String> configurations = getCandidateConfigurations(annotationMetadata,
                attributes);
        configurations = removeDuplicates(configurations);
        Set<String> exclusions = getExclusions(annotationMetadata, attributes);
        checkExcludedClasses(configurations, exclusions);
        configurations.removeAll(exclusions);
        configurations = filter(configurations, autoConfigurationMetadata);
        fireAutoConfigurationImportEvents(configurations, exclusions);
        return StringUtils.toStringArray(configurations);
    }
复制代码

能够看到第九行,它实际上是去加载 public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";外部文件。这个外部文件,有不少自动配置的类。以下:

其中,最关键的要属@Import(EnableAutoConfigurationImportSelector.class),借助EnableAutoConfigurationImportSelector,@EnableAutoConfiguration能够帮助SpringBoot应用将全部符合条件的@Configuration配置都加载到当前SpringBoot建立并使用的IoC容器。就像一只“八爪鱼”同样。

自动配置幕后英雄:SpringFactoriesLoader详解

借助于Spring框架原有的一个工具类:SpringFactoriesLoader的支持,@EnableAutoConfiguration能够智能的自动配置功效才得以大功告成!

SpringFactoriesLoader属于Spring框架私有的一种扩展方案,其主要功能就是从指定的配置文件META-INF/spring.factories加载配置。

public abstract class SpringFactoriesLoader {
    //...
    public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader classLoader) {
        ...
    }


    public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
        ....
    }
}
复制代码

配合@EnableAutoConfiguration使用的话,它更可能是提供一种配置查找的功能支持,即根据@EnableAutoConfiguration的完整类名org.springframework.boot.autoconfigure.EnableAutoConfiguration做为查找的Key,获取对应的一组@Configuration类

上图就是从SpringBoot的autoconfigure依赖包中的META-INF/spring.factories配置文件中摘录的一段内容,能够很好地说明问题。

因此,@EnableAutoConfiguration自动配置的魔法骑士就变成了:从classpath中搜寻全部的META-INF/spring.factories配置文件,并将其中org.springframework.boot.autoconfigure.EnableutoConfiguration对应的配置项经过反射(Java Refletion)实例化为对应的标注了@Configuration的JavaConfig形式的IoC容器配置类,而后汇总为一个并加载到IoC容器。

SpringBoot的启动原理基本算是讲完了,为了方便记忆,我根据上面的分析画了张图

深刻探索SpringApplication执行流程

SpringApplication的run方法的实现是咱们本次旅程的主要线路,该方法的主要流程大致能够概括以下:

1) 若是咱们使用的是SpringApplication的静态run方法,那么,这个方法里面首先要建立一个SpringApplication对象实例,而后调用这个建立好的SpringApplication的实例方法。在SpringApplication实例初始化的时候,它会提早作几件事情:

public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
        return new SpringApplication(sources).run(args);
    }
复制代码
  • 根据classpath里面是否存在某个特征类(org.springframework.web.context.ConfigurableWebApplicationContext)来决定是否应该建立一个为Web应用使用的ApplicationContext类型。

  • 使用SpringFactoriesLoader在应用的classpath中查找并加载全部可用的ApplicationContextInitializer。

  • 使用SpringFactoriesLoader在应用的classpath中查找并加载全部可用的ApplicationListener。

  • 推断并设置main方法的定义类。

    @SuppressWarnings({ "unchecked", "rawtypes" }) private void initialize(Object[] sources) { if (sources != null && sources.length > 0) { this.sources.addAll(Arrays.asList(sources)); } this.webEnvironment = deduceWebEnvironment(); setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class)); setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = deduceMainApplicationClass(); }

2) SpringApplication实例初始化完成而且完成设置后,就开始执行run方法的逻辑了,方法执行伊始,首先遍历执行全部经过SpringFactoriesLoader能够查找到并加载的SpringApplicationRunListener。调用它们的started()方法,告诉这些SpringApplicationRunListener,“嘿,SpringBoot应用要开始执行咯!”。

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        FailureAnalyzers analyzers = null;
        configureHeadlessProperty();
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                    args);
            ConfigurableEnvironment environment = prepareEnvironment(listeners,
                    applicationArguments);
            Banner printedBanner = printBanner(environment);
            context = createApplicationContext();
            analyzers = new FailureAnalyzers(context);
            prepareContext(context, environment, listeners, applicationArguments,
                    printedBanner);
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;// 核心点:会打印springboot的启动标志,直到server.port端口启动
            refreshContext(context);
            afterRefresh(context, applicationArguments);
            listeners.finished(context, null);
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass)
                        .logStarted(getApplicationLog(), stopWatch);
            }
            return context;
        }
        catch (Throwable ex) {
            handleRunFailure(context, listeners, analyzers, ex);
            throw new IllegalStateException(ex);
        }
    }
复制代码

3) 建立并配置当前Spring Boot应用将要使用的Environment(包括配置要使用的PropertySource以及Profile)。

private ConfigurableEnvironment prepareEnvironment(
            SpringApplicationRunListeners listeners,
            ApplicationArguments applicationArguments) {
        // Create and configure the environment
        ConfigurableEnvironment environment = getOrCreateEnvironment();
        configureEnvironment(environment, applicationArguments.getSourceArgs());
        listeners.environmentPrepared(environment);
        if (!this.webEnvironment) {
            environment = new EnvironmentConverter(getClassLoader())
                    .convertToStandardEnvironmentIfNecessary(environment);
        }
        return environment;
    }
复制代码

4) 遍历调用全部SpringApplicationRunListener的environmentPrepared()的方法,告诉他们:“当前SpringBoot应用使用的Environment准备好了咯!”。

public void environmentPrepared(ConfigurableEnvironment environment) {
        for (SpringApplicationRunListener listener : this.listeners) {
            listener.environmentPrepared(environment);
        }
    }
复制代码

5) 若是SpringApplication的showBanner属性被设置为true,则打印banner。

private Banner printBanner(ConfigurableEnvironment environment) {
        if (this.bannerMode == Banner.Mode.OFF) {
            return null;
        }
        ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader
                : new DefaultResourceLoader(getClassLoader());
        SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(
                resourceLoader, this.banner);
        if (this.bannerMode == Mode.LOG) {
            return bannerPrinter.print(environment, this.mainApplicationClass, logger);
        }
        return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
    }
复制代码

6) 根据用户是否明确设置了applicationContextClass类型以及初始化阶段的推断结果,决定该为当前SpringBoot应用建立什么类型的ApplicationContext并建立完成,而后根据条件决定是否添加ShutdownHook,决定是否使用自定义的BeanNameGenerator,决定是否使用自定义的ResourceLoader,固然,最重要的,将以前准备好的Environment设置给建立好的ApplicationContext使用。

7) ApplicationContext建立好以后,SpringApplication会再次借助Spring-FactoriesLoader,查找并加载classpath中全部可用的ApplicationContext-Initializer,而后遍历调用这些ApplicationContextInitializer的initialize(applicationContext)方法来对已经建立好的ApplicationContext进行进一步的处理。

@SuppressWarnings({ "rawtypes", "unchecked" })
    protected void applyInitializers(ConfigurableApplicationContext context) {
        for (ApplicationContextInitializer initializer : getInitializers()) {
            Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(
                    initializer.getClass(), ApplicationContextInitializer.class);
            Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
            initializer.initialize(context);
        }
    }
复制代码

8) 遍历调用全部SpringApplicationRunListener的contextPrepared()方法。

private void prepareContext(ConfigurableApplicationContext context,
            ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
            ApplicationArguments applicationArguments, Banner printedBanner) {
        context.setEnvironment(environment);
        postProcessApplicationContext(context);
        applyInitializers(context);
        listeners.contextPrepared(context);
        if (this.logStartupInfo) {
            logStartupInfo(context.getParent() == null);
            logStartupProfileInfo(context);
        }

        // Add boot specific singleton beans
        context.getBeanFactory().registerSingleton("springApplicationArguments",
                applicationArguments);
        if (printedBanner != null) {
            context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);
        }

        // Load the sources
        Set<Object> sources = getSources();
        Assert.notEmpty(sources, "Sources must not be empty");
        load(context, sources.toArray(new Object[sources.size()]));
        listeners.contextLoaded(context);
    }
复制代码

9) 最核心的一步,将以前经过@EnableAutoConfiguration获取的全部配置以及其余形式的IoC容器配置加载到已经准备完毕的ApplicationContext。

private void prepareAnalyzer(ConfigurableApplicationContext context,
            FailureAnalyzer analyzer) {
        if (analyzer instanceof BeanFactoryAware) {
            ((BeanFactoryAware) analyzer).setBeanFactory(context.getBeanFactory());
        }
    }
复制代码

10) 遍历调用全部SpringApplicationRunListener的contextLoaded()方法。

public void contextLoaded(ConfigurableApplicationContext context) {
        for (SpringApplicationRunListener listener : this.listeners) {
            listener.contextLoaded(context);
        }
    }
复制代码

11) 调用ApplicationContext的refresh()方法,完成IoC容器可用的最后一道工序。

private void refreshContext(ConfigurableApplicationContext context) {
        refresh(context);
        if (this.registerShutdownHook) {
            try {
                context.registerShutdownHook();
            }
            catch (AccessControlException ex) {
                // Not allowed in some environments.
            }
        }
    }
复制代码

12) 查找当前ApplicationContext中是否注册有CommandLineRunner,若是有,则遍历执行它们。

private void callRunners(ApplicationContext context, ApplicationArguments args) {
        List<Object> runners = new ArrayList<Object>();
        runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
        runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
        AnnotationAwareOrderComparator.sort(runners);
        for (Object runner : new LinkedHashSet<Object>(runners)) {
            if (runner instanceof ApplicationRunner) {
                callRunner((ApplicationRunner) runner, args);
            }
            if (runner instanceof CommandLineRunner) {
                callRunner((CommandLineRunner) runner, args);
            }
        }
    }
复制代码

13) 正常状况下,遍历执行SpringApplicationRunListener的finished()方法、(若是整个过程出现异常,则依然调用全部SpringApplicationRunListener的finished()方法,只不过这种状况下会将异常信息一并传入处理)

去除事件通知点后,整个流程以下:

public void finished(ConfigurableApplicationContext context, Throwable exception) {
        for (SpringApplicationRunListener listener : this.listeners) {
            callFinishedListener(listener, context, exception);
        }
    }
复制代码

自定义事件监听机制

SpringApplicationRunListener 接口规定了 SpringBoot 的生命周期,在各个生命周期广播相应的事件,调用实际的 ApplicationListener 类。经过对 SpringApplicationRunListener 的分析,也能够对 SpringBoot 的整个启动过程的理解会有很大帮助。

public interface SpringApplicationRunListener {
	//当run方法首次启动时当即调用。可用于很是早期的初始化。
	void starting();
	//在准备好环境后,但在建立ApplicationContext以前调用。
	void environmentPrepared(ConfigurableEnvironment environment);
	//在建立和准备好ApplicationContext以后,但在加载源以前调用。
	void contextPrepared(ConfigurableApplicationContext context);
	//在加载应用程序上下文后但刷新以前调用
	void contextLoaded(ConfigurableApplicationContext context);
	//上下文已刷新,应用程序已启动,但还没有调用commandlinerunner和applicationrunner。
	void started(ConfigurableApplicationContext context);
	//在运行方法完成以前当即调用,此时应用程序上下文已刷新,
	//而且全部commandlinerunner和applicationrunner都已调用。
	//2.0 才有
	void running(ConfigurableApplicationContext context);
	//在运行应用程序时发生故障时调用。2.0 才有
	void failed(ConfigurableApplicationContext context, Throwable exception);
}



ApplicationStartingEvent在运行开始时但在任何处理以前发送,侦听器和初始值设定项的注册除外。

ApplicationEnvironmentPreparedEvent在上下文中要使用的环境已知但在建立上下文以前发送。

ApplicationPreparedEvent将在刷新开始以前,但在加载bean定义以后发送。

ApplicationStartedEvent在刷新上下文以后,但在调用任何应用程序和命令行运行程序以前发送。

ApplicationreadyEvent在调用任何应用程序和命令行运行程序以后,将发送。它表示应用程序已准备好服务请求。

ApplicationFailedEvent若是启动时出现异常,则发送。
复制代码

ApplicationContextInitializer

package web.listeners;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author dongmei.tan
 * @date 2019/5/26 17:09
 */
public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        System.out.println("HelloApplicationContextInitializer...initialize...");
    }
}
复制代码

SpringApplicationRunListener

package web.listeners;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * @author dongmei.tan
 * @date 2019/5/26 17:16
 */
public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {

    public HelloSpringApplicationRunListener(SpringApplication application, String[] args) {

    }

    @Override
    public void starting() {
        System.out.println("SpringApplicationRunListener...starting.. ");
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        System.out.println("SpringApplicationRunListener...environmentPrepared..; ==environment" + environment);
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...contextPrepared..==> " + context);
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...starting..==> " + context);
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...started..==> " + context);
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...running..==> " + context);
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("SpringApplicationRunListener...failed..==> " + context);
    }
}
复制代码

配置(META-INF/spring.factories)

org.springframework.context.ApplicationContextInitializer=\
web.listeners.HelloApplicationContextInitializer

# Application Listeners
org.springframework.boot.SpringApplicationRunListener=\
web.listeners.HelloSpringApplicationRunListener
复制代码

只须要放在ioc容器中

ApplicationRunner

CommandLineRunner

若是在SpringApplication启动后须要运行一些特定的代码,能够实现ApplicationRunner或CommandLineRunner接口。两个接口以相同的方式工做,并提供一个单独的运行方法,该方法在SpringApplication.Run(…)完成以前调用。

commandLineRunner接口以简单的字符串数组形式提供对应用程序参数的访问,而applicationRunner使用前面讨论过的applicationArguments接口。下面的示例显示了带有run方法的commandlinerunner:




package web.listeners;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @author dongmei.tan
 * @date 2019/5/26 17:21
 */
@Component
public class HelloApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner...run..");
    }
}


package web.listeners;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @author dongmei.tan
 * @date 2019/5/26 17:22
 */
@Component
public class HelloCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner...run...");
    }
}
复制代码
相关文章
相关标签/搜索