spring 整合使用

Springboot自定义starter组件以及实现原理

2022-05-31  本文已影响0人  嘀嗒Pitaya

1 文章说明

1.1本文目标:

1.2 前置知识说明

1.2.1 关于starter组件的命名规范

2.实现starter组件

 <dependencies>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

2.1 基于条件装配来实现

2.1.1 starter组件定义

public interface IHelloService {
    String sayHello(String  name);
}
public class HelloServiceImpl implements IHelloService {

    private HelloProperties helloProperties;

    public HelloServiceImpl(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    @Override
    public String sayHello(String name) {
        return helloProperties.getName()+"say hello "+name;
    }
}
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {

    private String name;

    private boolean enable;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }
}
@Configuration
@EnableConfigurationProperties(value = HelloProperties.class)
public class HelloConfiguration {


    @Bean
    @ConditionalOnProperty("hello.enable")
    public IHelloService helloService(HelloProperties helloProperties){
        return new HelloServiceImpl(helloProperties);
    }
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.pitaya.starter.hello.HelloConfiguration

2.1.2 starter组件引用

<dependency>
    <groupId>org.example</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
hello:
  name: 笑笑
  enable: true 
@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private IHelloService helloService;

    @GetMapping("/say-hello")
    public String sayHello(String name){
        return helloService.sayHello(name);
    }
}

2.2 基于@import注解来实现

2.2.1 starter组件定义

public interface IMyService {
    String getName();
}

public class MyServiceImpl implements IMyService {

    private MyEnableProperties myEnableProperties;

    public MyServiceImpl(MyEnableProperties myEnableProperties) {
        this.myEnableProperties = myEnableProperties;
    }

    @Override
    public String getName() {
        return myEnableProperties.getName();
    }
}
@ConfigurationProperties(prefix = "myenable")
public class MyEnableProperties {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
@Configuration
@EnableConfigurationProperties(value = MyEnableProperties.class)
public class MyEnableConfiguration {

    @Bean
    public IMyService myService(MyEnableProperties myEnableProperties){
        return new MyServiceImpl(myEnableProperties);
    }
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(MyEnableConfiguration.class)
public @interface EnableHello {
}

2.2.2 starter组件引用

<dependency>
    <groupId>org.example</groupId>
    <artifactId>myenable-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
@EnableHello
@SpringBootApplication
public class MyEnableExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyEnableExampleApplication.class,args);
    }
}
myenable:
  name: aa

3.原理

3.1 启动前的准备工作

@SpringBootApplication
public class HelloExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloExampleApplication.class,args);
    }
}
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
    return new SpringApplication(primarySources).run(args);
}
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
   
    // resourceLoader参数为空
   this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
  
    // 将启动类放到Set结构的primarySources中
   this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
  
    // 判断当前web环境,结果为Servlet环境
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
  
    // 将结果存储到Application的属性initializers中,在后续需要的时候从这里读,获取到7个类的实例化
    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
  
    // 将结果存储到Application的属性listeners中,在后续需要的时候从这里读,获取到10个类的实例化
   setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
  
    //确定当前应用的启动类,就是你的springboot的main函数的入口
   this.mainApplicationClass = deduceMainApplicationClass();
}
public ConfigurableApplicationContext run(String... args) {

    //下面这两行代码就是为了记录启动停止时间之类的,和主流程没啥关系,所以不理它
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    //定义spring的容器,只是定义一个变量而已
    ConfigurableApplicationContext context = null;
 
   //创建异常报告器
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();

    // 主要设置java.awt.headless设置系统参数为true,用于运行xxx的图像处理,也是关系不大
    configureHeadlessProperty();

    // 创建了SpringApplicationRunListeners对象,里面包括一些监听器,例如EventPublishingRunListener 
    SpringApplicationRunListeners listeners = getRunListeners(args);

    //1.初始化EventPublishingRunListener
    //2.将application的11个监听器给当前对象
    // 上面只是简单的两个,其实还有一些东西的初始化,debug的时候如果某个对象不知道怎么来的,可以debug看看这些监听器 
   listeners.starting();
   try {
      //解析命令行参数
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);

      //准备环境    
      ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
      configureIgnoreBeanInfo(environment);

      // 打印banner图对象,没错,就是控制台的那个spring
      Banner printedBanner = printBanner(environment);
    
      // 创建spring环境,子类是AnnotationConfigServletWebServerApplicationContext,这一步也是至关重要,开始和Spring关联起来了
      context = createApplicationContext();
      
      //TODO 异常处理,先不管
      exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                    new Class[] { ConfigurableApplicationContext.class }, context);

      /// 准备Spring上下文
      // 1.向Spring注册bean internalConfigurationBeanNameGenerator
      // 2.添加数据类型转换器
      prepareContext(context, environment, listeners, applicationArguments, printedBanner);

      // 重点要弄明白的,会调用到spring的refresh()方法里面去,全部的spring启动过程,在这个方法呈现在你的面前
      refreshContext(context);

      // 空实现
      afterRefresh(context, applicationArguments);

      // 停止及时,你看到springboot启动完后耗时多久就是它咯 
      stopWatch.stop();
    
      // log相关,不理会
      if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
      }
    
     listeners.started(context);

      // ApplicationRunner的执行,包括你自己定义的
      callRunners(context, applicationArguments);
  }catch (Throwable ex) {
      handleRunFailure(context, ex, exceptionReporters, listeners);
      throw new IllegalStateException(ex);
  }

  try {
      listeners.running(context);
   }catch (Throwable ex) {
      handleRunFailure(context, ex, exceptionReporters, null);
       throw new IllegalStateException(ex);
  }
  return context;
}
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
            ApplicationArguments applicationArguments) {
        // Create and configure the environment
    // 创建环境
        ConfigurableEnvironment environment = getOrCreateEnvironment();
  
    //配置环境
        configureEnvironment(environment, applicationArguments.getSourceArgs());
        ConfigurationPropertySources.attach(environment);
  
    // 告诉所有监听器环境已经准备好,发布事件为ApplicationEnvironmentPreparedEvent,7个监听器会收到事件
        listeners.environmentPrepared(environment);
        bindToSpringApplication(environment);
        if (!this.isCustomEnvironment) {
            environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
                    deduceEnvironmentClass());
        }
        ConfigurationPropertySources.attach(environment);
        return environment;
    }
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
        if (this.addConversionService) {
        
          // 加载所有的类型转换器
            ConversionService conversionService = ApplicationConversionService.getSharedInstance();
            
      // 设置数据类型转换器到当前环境         environment.setConversionService((ConfigurableConversionService) conversionService);
        }
        
        // 配置properties资源
        configurePropertySources(environment, args);
        
        // 将properties加载到环境变量
        configureProfiles(environment, args);
    }
protected ConfigurableApplicationContext createApplicationContext() {
        Class<?> contextClass = this.applicationContextClass;
        if (contextClass == null) {
            try {
                // 在创建SpringApplication对象的时候就已经确定是SERVLET了
                switch (this.webApplicationType) { 
                case SERVLET:

                    // public static final String DEFAULT_SERVLET_WEB_CONTEXT_CLASS = "org.springframework.boot."+web.servlet.context.AnnotationConfigServletWebServerApplicationContext
                    // 可以知道Spring上下文就是AnnotationConfigServletWebServerApplicationContext
                    contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
                    break;
                case REACTIVE:
                    contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
                    break;
                default:
                    contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
                }
            }
            catch (ClassNotFoundException ex) {
                throw new IllegalStateException(
                        "Unable create a default ApplicationContext, please specify an ApplicationContextClass", ex);
            }
        }
        return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}

3.2 那个难懂的refresh()方法

3.2.1

protected void  invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {

//beanFactory, getBeanFactoryPostProcessors()可以获取到以下三个类
//CachingMetadataReaderFactoryPostProcessor
//ConfigurationWarningsPostProcessor
//PropertySourceOrderingPostProcessor
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
             
        // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
        // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
  
        if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
            beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
            beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
        }
}
public void parse(Set<BeanDefinitionHolder> configCandidates) {
        for (BeanDefinitionHolder holder : configCandidates) {
            BeanDefinition bd = holder.getBeanDefinition();
            try {
                if (bd instanceof AnnotatedBeanDefinition) {
                    parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
                }
                else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) {
                    parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());
                }
                else {
                    parse(bd.getBeanClassName(), holder.getBeanName());
                }
            }
            catch (BeanDefinitionStoreException ex) {
                throw ex;
            }
            catch (Throwable ex) {
                throw new BeanDefinitionStoreException(
                        "Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);
            }
        }

        // 这里需要处理所有延迟处理的@import上的类,例如AutoConfigurationImportSelector,在解析道@import上的类之后,这里正式去调用扫描jar包的spring.factories文件了
        this.deferredImportSelectorHandler.process();
    }
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
            throws IOException {

        //加载 compotent 注解的类
        if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
            // Recursively process any member (nested) classes first
            processMemberClasses(configClass, sourceClass);
        }

        // Process any @PropertySource annotations
        // 加载属性文件
        for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
                sourceClass.getMetadata(), PropertySources.class,
                org.springframework.context.annotation.PropertySource.class)) {
            if (this.environment instanceof ConfigurableEnvironment) {
                processPropertySource(propertySource);
            }
            else {
                logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
                        "]. Reason: Environment must implement ConfigurableEnvironment");
            }
        }

        // Process any @ComponentScan annotations
        //先找出类上的@ComponentScan和@ComponentScans注解的所有属性, 解析@ComponentScan和@ComponentScans配置的扫描的包所包含的类,如果没有就从启动类的包查找
        Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
                sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
        if (!componentScans.isEmpty() &&
                !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
            for (AnnotationAttributes componentScan : componentScans) {
                // The config class is annotated with @ComponentScan -> perform the scan immediately
                Set<BeanDefinitionHolder> scannedBeanDefinitions =
                        this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
                // Check the set of scanned definitions for any further config classes and parse recursively if needed
                for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
                    BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
                    if (bdCand == null) {
                        bdCand = holder.getBeanDefinition();
                    }
                    if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
                        parse(bdCand.getBeanClassName(), holder.getBeanName());
                    }
                }
            }
        }

        // Process any @Import annotations
        //处理Import注解注册的bean,这一步只会将import注册的bean变为ConfigurationClass,不会变成BeanDefinition
        processImports(configClass, sourceClass, getImports(sourceClass), true);

        // Process any @ImportResource annotations
        //处理@ImportResource注解引入的配置文件
        AnnotationAttributes importResource =
                AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
        if (importResource != null) {
            String[] resources = importResource.getStringArray("locations");
            Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
            for (String resource : resources) {
                String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
                configClass.addImportedResource(resolvedResource, readerClass);
            }
        }

        // Process individual @Bean methods
        //处理加了@Bean注解的方法
        Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
        for (MethodMetadata methodMetadata : beanMethods) {
            configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
        }

        // Process default methods on interfaces
        processInterfaces(configClass, sourceClass);

        // Process superclass, if any
        if (sourceClass.getMetadata().hasSuperClass()) {
            String superclass = sourceClass.getMetadata().getSuperClassName();
            if (superclass != null && !superclass.startsWith("java") &&
                    !this.knownSuperclasses.containsKey(superclass)) {
                this.knownSuperclasses.put(superclass, configClass);
                // Superclass found, return its annotation metadata and recurse
                return sourceClass.getSuperClass();
            }
        }

        // No superclass -> processing is complete
        return null;
    }

    /**
     * Register member (nested) classes that happen to be configuration classes themselves.
     */
    private void processMemberClasses(ConfigurationClass configClass, SourceClass sourceClass) throws IOException {
        Collection<SourceClass> memberClasses = sourceClass.getMemberClasses();
        if (!memberClasses.isEmpty()) {
            List<SourceClass> candidates = new ArrayList<>(memberClasses.size());
            for (SourceClass memberClass : memberClasses) {
                if (ConfigurationClassUtils.isConfigurationCandidate(memberClass.getMetadata()) &&
                        !memberClass.getMetadata().getClassName().equals(configClass.getMetadata().getClassName())) {
                    candidates.add(memberClass);
                }
            }
            OrderComparator.sort(candidates);
            for (SourceClass candidate : candidates) {
                if (this.importStack.contains(configClass)) {
                    this.problemReporter.error(new CircularImportProblem(configClass, this.importStack));
                }
                else {
                    this.importStack.push(configClass);
                    try {
                        processConfigurationClass(candidate.asConfigClass(configClass));
                    }
                    finally {
                        this.importStack.pop();
                    }
                }
            }
        }
    }
 processImports(configClass, sourceClass, getImports(sourceClass), true);
private Set<SourceClass> getImports(SourceClass sourceClass) throws IOException {
        Set<SourceClass> imports = new LinkedHashSet<>();
        Set<SourceClass> visited = new LinkedHashSet<>();

                // 收集当前类上所有的import载入的class
        collectImports(sourceClass, imports, visited);
        return imports;
    }
private void collectImports(SourceClass sourceClass, Set<SourceClass> imports, Set<SourceClass> visited)
            throws IOException {

        if (visited.add(sourceClass)) {
            for (SourceClass annotation : sourceClass.getAnnotations()) {
                String annName = annotation.getMetadata().getClassName();
                if (!annName.equals(Import.class.getName())) {
                    collectImports(annotation, imports, visited);
                }
            }
            imports.addAll(sourceClass.getAnnotationAttributes(Import.class.getName(), "value"));
        }
    }
this.deferredImportSelectorHandler.process();
public void process() {
            List<DeferredImportSelectorHolder> deferredImports = this.deferredImportSelectors;
            this.deferredImportSelectors = null;
            try {
                if (deferredImports != null) {
                    DeferredImportSelectorGroupingHandler handler = new DeferredImportSelectorGroupingHandler();
                    deferredImports.sort(DEFERRED_IMPORT_COMPARATOR);
                    deferredImports.forEach(handler::register);
                    handler.processGroupImports();
                }
            }
            finally {
                this.deferredImportSelectors = new ArrayList<>();
            }
        }
public void processGroupImports() {
            for (DeferredImportSelectorGrouping grouping : this.groupings.values()) {
           //grouping.getImports()就会调用
                grouping.getImports().forEach(entry -> {
                    ConfigurationClass configurationClass = this.configurationClasses.get(
                            entry.getMetadata());
                    try {
                        processImports(configurationClass, asSourceClass(configurationClass),
                                asSourceClasses(entry.getImportClassName()), false);
                    }
                    catch (BeanDefinitionStoreException ex) {
                        throw ex;
                    }
                    catch (Throwable ex) {
                        throw new BeanDefinitionStoreException(
                                "Failed to process import candidates for configuration class [" +
                                        configurationClass.getMetadata().getClassName() + "]", ex);
                    }
                });
            }
        }
public Iterable<Group.Entry> getImports() {
            for (DeferredImportSelectorHolder deferredImport : this.deferredImports) {
            
this.group.process(deferredImport.getConfigurationClass().getMetadata(),
                        deferredImport.getImportSelector());
            }
 //调用import的selectImports()方法
            return this.group.selectImports();
        }
protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata 
autoConfigurationMetadata, AnnotationMetadata annotationMetadata) {
        if (!this.isEnabled(annotationMetadata)) {
            return EMPTY_ENTRY;
        } else {
            AnnotationAttributes attributes = this.getAttributes(annotationMetadata);

            // 遍历jar包读取spring.factories文件中的所有的类
            List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
            configurations = this.removeDuplicates(configurations);
            Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
           
            //处理一些相关的排除的不需要的类
            this.checkExcludedClasses(configurations, exclusions);
            configurations.removeAll(exclusions);
            configurations = this.filter(configurations, autoConfigurationMetadata);
            this.fireAutoConfigurationImportEvents(configurations, exclusions);
            return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
        }
    }
this.reader.loadBeanDefinitions(configClasses);

总结

Spring到底如何完成自动装配?简单点,应该是以下几个步骤:

上一篇 下一篇

猜你喜欢

热点阅读