Spring源码研究二

spring

前沿:感觉所有重点都在图里面了
spring源码研究

需要懂的技术论点

  • springbootstrap方法执行
  • 类加载过程
  • proxy的代理模式
  • relection框架的过程
  • spring的模块包干的事情

listeners.starting()干的事情

这一块 自己都没特别看懂🤦‍♂️,还是以类图的层次来主要分析吧
springbootlistener的构造图

概念总结:
SpringApplicationRunListener:通过调用SimpleApplicationEventMulticaster类型的启动
SimpleApplicationEventMulticaster:一个简单的事件启动类,拥有简单的启动功能,和容器的存取功能
EventPublishingRunListener:
上述类的关系有阐述清楚,SpringApplicationRunListeners中有关于SpringApplicationRunListener容器,EventPublishingRunListener 是 上面runListener的具体实现类,它拥有SimpleApplicationEventMulticaster并且通过他去启动。

SpringApplicationRunListener

主要就是

SpringApplicationRunListener listener = (SpringApplicationRunListener)var1.next();
            listener.starting()

并且在SpringApplicationRunListener里面有一段

public void starting() {
        this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));
    }

这个就是核心。
继续进入里面分析

public void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType) {
        ResolvableType type = eventType != null ? eventType : this.resolveDefaultEventType(event);
        Iterator var4 = this.getApplicationListeners(event, type).iterator();

        while(var4.hasNext()) {
            ApplicationListener<?> listener = (ApplicationListener)var4.next();
            Executor executor = this.getTaskExecutor();
            if (executor != null) {
                executor.execute(() -> {
                    this.invokeListener(listener, event);
                });
            } else {
                this.invokeListener(listener, event);
            }
        }

    }

这段代码里面 有两个核心,里面的值从哪里来,方法真正的执行是什么。

  1. 第一个值从哪里来
    this.getApplicationListeners(event, type).iterator();
protected Collection<ApplicationListener<?>> getApplicationListeners(ApplicationEvent event, ResolvableType eventType) {
        Object source = event.getSource();
        Class<?> sourceType = source != null ? source.getClass() : null;
        AbstractApplicationEventMulticaster.ListenerCacheKey cacheKey = new AbstractApplicationEventMulticaster.ListenerCacheKey(eventType, sourceType);
        AbstractApplicationEventMulticaster.ListenerRetriever retriever = (AbstractApplicationEventMulticaster.ListenerRetriever)this.retrieverCache.get(cacheKey);
        if (retriever != null) {
            return retriever.getApplicationListeners();
        } else if (this.beanClassLoader == null || ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) && (sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader))) {
            synchronized(this.retrievalMutex) {
                retriever = (AbstractApplicationEventMulticaster.ListenerRetriever)this.retrieverCache.get(cacheKey);
                if (retriever != null) {
                    return retriever.getApplicationListeners();
                } else {
                    retriever = new AbstractApplicationEventMulticaster.ListenerRetriever(true);
                    Collection<ApplicationListener<?>> listeners = this.retrieveApplicationListeners(eventType, sourceType, retriever);
                    this.retrieverCache.put(cacheKey, retriever);
                    return listeners;
                }
            }
        } else {
            return this.retrieveApplicationListeners(eventType, sourceType, (AbstractApplicationEventMulticaster.ListenerRetriever)null);
        }
    }

AbstractApplicationEventMulticaster专门为,事件定义个一个私有类,在里面进行存取的操作。里面的核心就是
ListenerRetriever.applicationListeners和ListenerRetriever.applicationListeners.applicationListenerBeans这两个可爱的小东东哈
2. 方法真正的执行是什么
this.invokeListener(listener, event)

protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event) {
        ErrorHandler errorHandler = this.getErrorHandler();
        if (errorHandler != null) {
            try {
                this.doInvokeListener(listener, event);
            } catch (Throwable var5) {
                errorHandler.handleError(var5);
            }
        } else {
            this.doInvokeListener(listener, event);
        }

    }
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
        try {
            listener.onApplicationEvent(event);
        } catch (ClassCastException var6) {
            String msg = var6.getMessage();
            if (msg != null && !this.matchesClassCastMessage(msg, event.getClass())) {
                throw var6;
            }

            Log logger = LogFactory.getLog(this.getClass());
            if (logger.isTraceEnabled()) {
                logger.trace("Non-matching event type for listener: " + listener, var6);
            }
        }

    }

这两个方法,最后listener.onApplicationEvent(event)怎么去分析,那就太多了,因为不同的实现,有不同的具体逻辑就不继续看了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值