ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

10.spring源码

2022-05-08 21:33:09  阅读:181  来源: 互联网

标签:10 beanFactory spring BeansException beanName refresh bean 源码 public


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的详情为:
    未完待续...

 

标签:10,beanFactory,spring,BeansException,beanName,refresh,bean,源码,public
来源: https://www.cnblogs.com/wmd-l/p/16246980.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有