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()。
AbstractApplicationContext在这就相当于一个模板, 他定制了ApplicationContext初始化的所有流程. 而refresh则是整个流程的核心.
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(); } } }
refesh中的核心是通过obtainFreshBeanFactory()获取了BeanFactory,另外就是初始化一些边缘化的功能。
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,注解扫描等),会有不同的实现,只是细节不同,就不多阐述了。
上面主要描述了ApplicationContext的启动流程,接下来我们来分析一下getBean的流程。
getBean是属于BeanFactory体系的核心功能。我们先来看一下BeanFactory的家族谱。
我们开始就有提到的BeanDefinition, 在启动过程中,是已经被全部载入的了。
getBean则是根据这些BeanDefinition来生成真正的Bean,
如果Scope为Singleton, 则缓存起来,以备下次重用。 (Scope域主要分为Singleton(单例, 默认),Prototype(原型))
如果没有对应的BeanDefinition则直接委托双亲BeanFactory.getBean。代码如下
@SuppressWarnings("unchecked") protected <T> T doGetBean( final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { final String beanName = transformedBeanName(name); Object bean; // Eagerly check singleton cache for manually registered singletons. Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) { if (logger.isDebugEnabled()) { if (isSingletonCurrentlyInCreation(beanName)) { logger.debug("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference"); } else { logger.debug("Returning cached instance of singleton bean '" + beanName + "'"); } } bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else { // Fail if we're already creating this bean instance: // We're assumably within a circular reference. if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } // Check if bean definition exists in this factory. BeanFactory parentBeanFactory = getParentBeanFactory(); if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. String nameToLookup = originalBeanName(name); if (args != null) { // Delegation to parent with explicit args. return (T) parentBeanFactory.getBean(nameToLookup, args); } else { // No args -> delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup, requiredType); } } if (!typeCheckOnly) { markBeanAsCreated(beanName); } try { final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); // Guarantee initialization of beans that the current bean depends on. String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { for (String dependsOnBean : dependsOn) { if (isDependent(beanName, dependsOnBean)) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dependsOnBean + "'"); } registerDependentBean(dependsOnBean, beanName); getBean(dependsOnBean); } } // Create bean instance. if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { try { return createBean(beanName, mbd, args); } catch (BeansException ex) { // Explicitly remove instance from singleton cache: It might have been put there // eagerly by the creation process, to allow for circular reference resolution. // Also remove any beans that received a temporary reference to the bean. destroySingleton(beanName); throw ex; } } }); bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } else if (mbd.isPrototype()) { // It's a prototype -> create a new instance. Object prototypeInstance = null; try { beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } else { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); if (scope == null) { throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'"); } try { Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } } }); bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; consider " + "defining a scoped proxy for this bean if you intend to refer to it from a singleton", ex); } } } catch (BeansException ex) { cleanupAfterBeanCreationFailure(beanName); throw ex; } } // Check if required type matches the type of the actual bean instance. if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) { try { return getTypeConverter().convertIfNecessary(bean, requiredType); } catch (TypeMismatchException ex) { if (logger.isDebugEnabled()) { logger.debug("Failed to convert bean '" + name + "' to required type [" + ClassUtils.getQualifiedName(requiredType) + "]", ex); } throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } return (T) bean; }
接下是真正的creatBean,流程有:
创建(createBeanInstance),
装配(populateBean, 注入依赖属性),
初始化(initializeBean, 初始化,触发已注册的钩子函数),
返回Bean
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) { // Instantiate the bean. BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null); Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null); // Allow post-processors to modify the merged bean definition. synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); mbd.postProcessed = true; } } // Eagerly cache singletons to be able to resolve circular references // even when triggered by lifecycle interfaces like BeanFactoryAware. boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); if (earlySingletonExposure) { if (logger.isDebugEnabled()) { logger.debug("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references"); } addSingletonFactory(beanName, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { return getEarlyBeanReference(beanName, mbd, bean); } }); } // Initialize the bean instance. Object exposedObject = bean; try { populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { exposedObject = initializeBean(beanName, exposedObject, mbd); } } catch (Throwable ex) { if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { throw (BeanCreationException) ex; } else { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); } } if (earlySingletonExposure) { Object earlySingletonReference = getSingleton(beanName, false); if (earlySingletonReference != null) { if (exposedObject == bean) { exposedObject = earlySingletonReference; } else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) { String[] dependentBeans = getDependentBeans(beanName); Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length); for (String dependentBean : dependentBeans) { if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) { actualDependentBeans.add(dependentBean); } } if (!actualDependentBeans.isEmpty()) { throw new BeanCurrentlyInCreationException(beanName, "Bean with name '" + beanName + "' has been injected into other beans [" + StringUtils.collectionToCommaDelimitedString(actualDependentBeans) + "] in its raw version as part of a circular reference, but has eventually been " + "wrapped. This means that said other beans do not use the final version of the " + "bean. This is often the result of over-eager type matching - consider using " + "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example."); } } } } // Register bean as disposable. try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }
总结:
spring核心功能IOC就是用BeanFactory的形式实现的,他全权接管bean的创建,组装以及生命周期的管理。
BeanFactory的双亲设计模型,能让多BeanFactory场景更好的组装。
BeanFactory的自动装配,单例重用,面向接口编程,各种设计模式的灵活运用。
bean托管分为两步,首先是作为BeanDefintion被载入,直到getBean时才会被真正初始化。
设计方面呢,spring良好的继承体系,使扩展变得十分容易。自定义一个自己想要的BeanFactory易如反掌。