微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

10.spring源码

1.spring-aop底层就是动态代理,例如有两个切面类(A_Aspect和B_Aspect)同时切目标方法

    A_Aspect{
        try{
            @Before(前置通知)
            mthod.invoke(obj,args)或是环绕通知的pjp.procced(args){------------------->此时并没有真正的执行目标方法,而是执行B_Aspect的切面方法
                B_Aspect{
                    try{
                         @Before(前置通知)
                        method.invoke(obj,args)或环绕通知的pjp.procced(args)---------------->此时是真正的执行目标方法
                        @afterreturning(返回通知)
                    }catch{
                        @afterthrowing(异常通知)
                    }finally{
                        @after(后置通知)
                    }
                }
             //B_Aspect切面方法执行完毕
             @afterthrowing(异常通知)  
        }catch{
            @afterthrowing(异常通知)
        }finally{
            @after(后置通知)
        }
    }



1.当我们创建spring容器时
    ApplicationContext ioc=new ClasspathXmlApplicationContext("ioc.xml");
2.会调用ClasspathXmlApplicationContext的三个参数构造器

public ClasspathXmlApplicationContext(String configLocation) throws BeansException {
    this(new String[]{configLocation}, true, (ApplicationContext)null);
}

对应的三个参数构造器代码如下:此时第二个参数refresh是固定为true
    public ClasspathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException {
        super(parent);
        this.setConfigLocations(configLocations);
        if (refresh) {
            //调用this.refresh();后,所有的单实例bean创建完成
            this.refresh();-------------------------------------->调用this.refresh();后,所有的单实例bean创建完成
        }
    }
    
对应的refresh()方法内容如下:
    public void refresh() throws BeansException, IllegalStateException {
    synchronized(this.startupShutdownMonitor) {----------------------->同步锁。保证多线程情况下ioc容器只被创建一次!
        //准备容器刷新:跳过此句后会打印以下内容:发现其刷新spring容器
        //八月 17, 2020 10:14:04 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
        //信息: Refreshing org.springframework.context.support.ClasspathXmlApplicationContext@612fc6eb: startup date [Mon Aug 17 22:14:04 CST 2020]; root of context hierarchy
        this.prepareRefresh();
        //spring解析xml配置文件,将要创建的所有bean的配置信息保存起来(此后ioc.xml配置文件没有用了,但是此时还没有创建组件,只是将)
        ConfigurableListablebeanfactory beanfactory = this.obtainFreshbeanfactory();
        this.preparebeanfactory(beanfactory);
        try {
            this.postProcessbeanfactory(beanfactory);
            this.invokebeanfactoryPostProcessors(beanfactory);
            this.registerBeanPostProcessors(beanfactory);
            //支持国际化
            this.initMessageSource();
            this.initApplicationEventMulticaster();
            //留给子类的方法
            this.onRefresh();
            this.registerListeners();
            //创建所有的单实例bean
            this.finishbeanfactoryInitialization(beanfactory);
            this.finishRefresh();
        } catch (BeansException var5) {
            this.destroyBeans();
            this.cancelRefresh(var5);
            throw var5;
        }

    }
}

对应的finishbeanfactoryInitialization(beanfactory);方法详情
protected void finishbeanfactoryInitialization(ConfigurableListablebeanfactory beanfactory) {
    if (beanfactory.containsBean("conversionService") && beanfactory.isTypeMatch("conversionService", ConversionService.class)) {
        beanfactory.setConversionService((ConversionService)beanfactory.getBean("conversionService", ConversionService.class));
    }

    beanfactory.setTempClassLoader((ClassLoader)null);
    beanfactory.freezeConfiguration();
    //初始化所有的单实例bean
    beanfactory.preInstantiateSingletons();
}



对应的beanfactory.preInstantiateSingletons();方法详情在DefaultListablebeanfactory类中
public void preInstantiateSingletons() throws BeansException {
    if (this.logger.isInfoEnabled()) {
        this.logger.info("Pre-instantiating singletons in " + this);
    }

    synchronized(this.beanDeFinitionMap) {
        List<String> beanNames = new ArrayList(this.beanDeFinitionNames);
        Iterator var4 = beanNames.iterator();

        while(true) {
            while(true) {
                String beanName;
                RootBeanDeFinition bd;
                do {
                    do {
                        do {
                            if (!var4.hasNext()) {
                                return;
                            }

                            beanName = (String)var4.next();
                            bd = this.getMergedLocalBeanDeFinition(beanName);
                        } while(bd.isAbstract());----------------------------------->bean是抽象类
                    } while(!bd.isSingleton());-------------------------------------->bena不是单实例
                } while(bd.isLazyInit());-------------------------------------------->bena是懒加载

                if (this.isfactorybean(beanName)) {
                    final factorybean factory = (factorybean)this.getBean("&" + beanName);
                    boolean isEagerInit;
                    if (System.getSecurityManager() != null && factory instanceof Smartfactorybean) {
                        isEagerInit = (Boolean)AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
                            public Boolean run() {
                                return ((Smartfactorybean)factory).isEagerInit();
                            }
                        }, this.getAccessControlContext());
                    } else {
                        isEagerInit = factory instanceof Smartfactorybean && ((Smartfactorybean)factory).isEagerInit();
                    }

                    if (isEagerInit) {
                        this.getBean(beanName);
                    }
                } else {
                    this.getBean(beanName);
                }
            }
        }
    }
}

this.getBean(beanName);方法具体详情
public Object getBean(String name) throws BeansException {
    return this.doGetBean(name, (Class)null, (Object[])null, false);
}


调用的dogetbean的详情为:
    未完待续...

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐