spring源码解析三(源码阅读先导篇)

2020-05-14  本文已影响0人  为梦想前进

上一期,我们简单分析了下spring的@Autowired注解的原理,这一次,我们再次分析spring的源码,大家还接的我们最初使用spring的时候
都是要在配置文件配置bean,然后才能使用,现在慢慢的这个xml的配置方式已经淘汰了,现在基本上都是使用springboot了,这次那,为了看
源码方便,我们就从最开始的配置文件加载来分析下

大家可以看到以下代码,我将springApplication.run方法注释掉了,就专门定义一个xml配置文件,然后加载这个配置文件,看看spring怎样一步步
完成容器的创建,属性赋值操作,等等

@SpringBootApplication
@MapperScan("com.ryx.xiaoxin_distribute.mapper")
@EnableCaching
@EnableTransactionManagement
public class XiaoxinDistributeApplication {


    public static void main(String[] args) {
        //SpringApplication.run(XiaoxinDistributeApplication.class, args);
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:application-hellospring.xml");

        System.out.println("applicationContext启动了....");

        // 从上下文中获取bean
        HelloSpring helloSpring =  applicationContext.getBean(HelloSpringImpl.class);

        System.out.println(helloSpring.sayHello());
    }

}

#Xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <bean id="helloSpring" class="com.ryx.xiaoxin_distribute.spring.springIocAndAop.IOC.test1.HelloSpringImpl"/>
</beans>

在aplicationContext的继承体系中,我们会发现aplicationContext继承了BeanFactory,所以我们也可以说ApplicationContext就是BeanFactory
BeanFactory到底是什么,你翻译成Bean工厂,或者Bean容器,都没有问题,其实就是为我们生产各种我们需要的Bean,让我们使用,在生产的过程中,
要处理各种依赖问题,所以又引入了依赖注入,其实就是解决bean在生成我们需要的bean的时候为了解决依赖问题所产生的的一种处理方式
试想一下,在没有BeanFactory的时候,对象之间的依赖关系,紧紧的耦合在一起,这种依赖关系必须我们自己维护,然后就是各种new,但是spring出现之后
就告诉我们,你不用管各个类之间的依赖了,我来帮你搞定,你只需要关注业务逻辑即可,还有一点,最最重要的,我们终于可以不用new了,因为都可以通过
beanFactory实例化好了,还有一点,beanFctory在实例化bean的过程中(生命周期内),为我们提供了各种街口,让我们有能力在bean的生命周期内为所欲为

总结下:
1:applicationContext就是一个beanFactory
2:beanFactory为我们解决了各个对象之间的依赖关系,达到松耦合的目的
3:我们不用在new对象了,
4:可以通过spring提供的各种接口在bean的生命周期内对bean做各种啊操作

接下来,我们跟着bean的启动来一探究竟

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


public ClassPathXmlApplicationContext(
            String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
            throws BeansException {

        //初始化父类content
        super(parent);
        //设置xml的配置路径
        setConfigLocations(configLocations);
        //刷新上下文
        if (refresh) {
            refresh();
        }
    }

@Override
    public void refresh() throws BeansException, IllegalStateException {
        //使用同步锁机制,防止容器并发刷新
        synchronized (this.startupShutdownMonitor) {
            //一: 预刷新操作,1:记录容器启动的时间2:处理属性的占位符3:校验属性是否可以解析
            prepareRefresh();

            // 二:定义bean工厂解析xml文件中的bean内容为map(BeandefinitionMap)的形式并将其注册到beanFctory中于格式
                        //key:helloSprin value:com.ryx.xiaoxin_distribute.spring.springIocAndAop.IOC.test1.HelloSpringImpl
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // 三:设置类加载器,手动注册系统属性的bean
            prepareBeanFactory(beanFactory);

            try {
                //四:加载BeanFactoryPostProcessor ,对bean工厂执行后置处理
                postProcessBeanFactory(beanFactory);

                // 五:加载实现了beanFactoryPostProcessors的方法
                invokeBeanFactoryPostProcessors(beanFactory);

                // 六:注册并实例化所有实现了BeanPostProcessor接口的实现类
                registerBeanPostProcessors(beanFactory);

                // 七:初始化MessageSource
                initMessageSource();

                // 八:为此上下文初始化事件多播器
                initApplicationEventMulticaster();

                //九:在特定上下文子类中初始化其他特殊bean
                onRefresh();

                // 十:注册监听器bean
                registerListeners();

                // 十一:初始化所有的单例bean
                finishBeanFactoryInitialization(beanFactory);

                // 十二:发布相应的事件,初始化完成
                finishRefresh();
            }

            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
                }

                // 十三:
                destroyBeans();

                // 十四:
                cancelRefresh(ex);

                // Propagate exception to caller.
                throw ex;
            }

            finally {
                // Reset common introspection caches in Spring's core, since we
                // might not ever need metadata for singleton beans anymore...
                resetCommonCaches();
            }
        }
    }

可以看到,spring的12个比较重要的方法,我都简单注明了下属性,本想在一篇文章写完这些步骤,但是内容实在太多,我会按照一个步骤一篇文章的形式,来分析spring的源码,希望我的文章会对你有一点点的帮助,本期文章是一个先导片,下一篇开始,我会具体分析spring的步骤哈!
Thanks!

上一篇下一篇

猜你喜欢

热点阅读