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

 

posted @ 2022-05-08 21:20  努力的达子  阅读(54)  评论(0编辑  收藏  举报