首页 > 代码库 > The spring, whose spring? my spring!

The spring, whose spring? my spring!

Spring在Java界也算是耳熟能详了。不久前,身为小菜的我,还在嫌弃他的繁琐,甚至认为他是多余的。

"根本不知道要他干嘛,除了带给我们一堆配置文件。"

可好像有人说过,存在即合理。

也是,他能这么流行,肯定是有他的原因的,何况他貌似还越来越火。(spring全家桶已经向我们袭来)  

Spring框架,其实说白了他就是一个工具,工具都是为人民服务的,那么他提供了哪些服务呢?

Spring核心服务:

  1,控制反转(invesion of control)

  2,面向切面(Aspect Oriented Programming)

 

何为控制反转? 其实又叫依赖注入(dependency injection)。说简单一点,就是将依赖交给spring管理。

面向切面编程呢,其实是对面向对象的一种强大补充,你可以把看做一种黑魔法,可以无死角,全方位嵌入程序,且无副作用。

 

我来给大家讲一个故事吧,从前有一个叫spring的勇士,他立志要给村里的程序员们带来春天。

他聪明地想到,程序员们讨厌什么,我帮他们做,不就好了吗。

终于他发现有个叫依赖的坏蛋,程序员最讨厌与他打交道了,因为他严重影响了程序员们的扩展与维护工作。

于是spring发明了一个BeanFactory,用这个特殊的工厂去替程序们管理依赖。

管理的方式是,你们要啥对象来我工厂拿,我帮你们组装好,处理好依赖。

渐渐的,托管的程序员越来越多,spring发明的BeanFactory好像过于简陋。

于是spring又发明了一个ApplicationContext,这是一个功能齐全的有专业人打理的现代工厂。

从此,村子里的程序员们过上了没羞没臊的幸福生活。

 

 我们来主要介绍下故事中的各大主角,首先我们来看看主角的家族谱。

 

突然想起之前有和产品聊过,需求如何呈现的事情。作为开发的我希望能图解加注释,将需求全方位展示出来。

如今写个技术博客我都不能好好的表达自己,无论是用图用文字还是用代码。

是的,幼稚的我,总幻想一个完美的世界在等待着我,却总是忽略自己的不完美。

 

现实中总是会有些人,可能不会那么引人注目,也不会那么卓尔不群,可他们一直默默付出,承担起了社会所给与的大部分责任,于他们,称之为英雄也不为过。

我们要主要剖析的也是这种英雄: 

ApplicationContext家族的AbstractApplicationContext;

BeanFactory家族中的DefaultListableBeanFactory;

 

AbstractApplicationContext作为家族的第一个实干主义。(第一个类,虽然是抽象的).

 家族赋予它的大部分期望他都实现了。如BeanFactory继承体系的所有实现。

在这是委托getBeanFactory()实现的。这应该有桥接的意思在里面。BeanFactory继承体系专职实现bean工厂,ApplicationContext体系也算是一种横向扩展。

首先看看ApplicationContext启动过程(这里引用的是XmlWebApplicationContext启动过程)

ConfigurableWebApplicationContext wac = createWebApplicationContext(servletContext);

wac.setId(idParam);

wac.setServletContext(sc);

String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (configLocationParam != null) {
    wac.setConfigLocation(configLocationParam);
}

wac.refresh();

是的,ApplicationContext在被构造后,设置一些基本信息,然后refresh()。

refresh就是实干家AbstractApplicationContext实现的。

public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            prepareRefresh();

            // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);

                // Initialize message source for this context.
                initMessageSource();

                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.
                onRefresh();

                // Check for listener beans and register them.
                registerListeners();

                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);

                // Last step: publish corresponding event.
                finishRefresh();
            }

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

                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();

                // Reset ‘active‘ flag.
                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();
            }
        }
    }

这里主要获取了BeanFactory,然后就是初始化的一些相关任务了。亮点就在obtainFreshBeanFactory.

obtainFreshBeanFactory通过refreshBeanFactory来获取真正的BeanFactory。

它的主要任务就是获取DefaultListableBeanFactory, 初始化他,并根据配置将BeanDefinition载入BeanFactory。

我们也贴出相关代码

protected final void refreshBeanFactory() throws BeansException {
        if (hasBeanFactory()) {
            destroyBeans();
            closeBeanFactory();
        }
        try {
            DefaultListableBeanFactory beanFactory = createBeanFactory();
            beanFactory.setSerializationId(getId());
            customizeBeanFactory(beanFactory);
            loadBeanDefinitions(beanFactory);
            synchronized (this.beanFactoryMonitor) {
                this.beanFactory = beanFactory;
            }
        }
        catch (IOException ex) {
            throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
        }
    }
loadBeanDefinitions()就是用来载入BeanDefinition。 
BeanDefinition也是一个不得不说的重要角色,他作为类的定义,抽象了类的基本数据以及一些依赖关系。
具体如何载入,根据不同形式,(如xml,注解扫描等),会有不同的实现,只是细节不同,就不多阐述了。

未完。。。

 

The spring, whose spring? my spring!