- 浏览: 50448 次
- 性别:
- 来自: 南京
文章分类
最新评论
本章探讨beanDefinition到instance的过程-注册单例实例到容器singletonObjects中
从xml到document
从document到beanDefinition
从beanDefiniton到instance
本文核心包括2部分:
4.1.实例化
4.2.初始化
这部分可以看到bean设置属性值及执行方法的顺序
从beanDefinition到instance
从beanDefinition到instance的上下文入口,也是在
abstractApplicationContext.refresh:(abstractApplicationContext implements ConfigurableWebApplicationContext)
具体查看:
<spring源码学习系列2.1-从xml到document>
abstractApplicationContext#refresh
其中
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
实例化beanDefinition定义的bean
abstractApplicationContext#finishBeanFactoryInitialization
这里的CONVERSION_SERVICE_BEAN_NAME是何时定义的?
// Instantiate all remaining (non-lazy-init) singletons.
beanFactory.preInstantiateSingletons();
此处实例化操作委托给beanFactory-由此可见,applicationContext的核心还是beanFactory
applicationContext本身已经实现了ListableBeanFactory, HierarchicalBeanFactory等接口,为什么还要另在里面定义一个AbstractRefreshableApplicationContext.beanFactory(DefaultListableBeanFactory)?
其实跟踪代码可知,appContext实现了ListableBeanFactory, HierarchicalBeanFactory接口。最终也是调用getBeanFactory()来实现其功能。
这样一来可以为spring瘦身,不必把DefaultListableBeanFactory实现的功能在spring中,有写一遍
二来让spring看起来也像一个容器BeanFactory,只不过更高级点,实现了其他beanFactory没有的功能
applicationContext
-------------------------------------------------------承上启下的分割线----------------------------------------------
defaultListableBeanFactory
真正的实例化beanDefinition还是由beanFactory来实现
DefaultListableBeanFactory的源码:
http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-beans/3.2.7.RELEASE/org/springframework/beans/factory/support/DefaultListableBeanFactory.java/
创建RootBeanDefinition
defaultListableBeanFactory#preInstantiateSingletons
这里开始循环实例化beanDefinition的bean,
首先判断是否是factoryBean,这里假设为非factoryBean,现实情况也是非factoryBean居多
DefaultListableBeanFactory开始将创建bean的任务给父类AbstractBeanFactory-spring的单一职责原则
defaultListableBeanFactory
创建rootBeanDefinition
-------------------------------------------------------父子类不同职责的分割线----------------------------------------------
abstractBeanFactory
getBean()
1.判断instance是否已经存在,存在则返回
2.获取RootBeanDefinition 委托AbstractAutowireCapableBeanFactory创建
abstractBeanFactory#getBean(String name):
abstractBeanFactory#doGetBean(final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
该方法代码量较大,读者可直接查看源代码,这里对方法做的事做分段简单描述:
1.判断该类是否已实例化// Eagerly check singleton cache for manually registered singletons.
abstractBeanFactory#doGetBean
这里又两部分操作:
一是判断是否正在生成或已经生成
二是根据上一步得到的是真正的bean instance还是factoryBean,返回真正的bean instance
defaultSingletonBeanRegistry#getSingleton:(AbstractBeanFactory继承了DefaultSingletonBeanRegistry)
earlySingletonObjects添加数据
singletonFactories删除数据
这里有好几个重要的属性(数据结构):-注意观察何时添加数据何时
删除数据及存放什么类型的数据
doGetBean的过程就是相当于对这些数据结构就行操作,而向其添加删除修改数据的过程就是算法
this.singletonObjects换成普通的HashMap<String, Object>可以吗?
DefaultSingletonBeanRegistry
/** Cache of singleton objects: bean name --> bean instance */
Map<String, Object> singletonObjects
单例列表-一级缓存
/** Cache of singleton factories: bean name --> ObjectFactory */
Map<String, ObjectFactory<?>> singletonFactories
生成单例的工厂缓存-三级缓存
/** Cache of early singleton objects: bean name --> bean instance */
Map<String, Object> earlySingletonObjects
早期单例列表-二级缓存
这里的三级Cache是spring用来解决循环引用而设计的
参考:http://www.jianshu.com/p/6c359768b1dc
最早是添加3级缓存数据
addSingletonFactory(beanName, new ObjectFactory<Object>() {
@Override public Object getObject() throws BeansException {
return getEarlyBeanReference(beanName, mbd, bean);
}});
对于第一次生成实例而言,此处3个Cache都为空
为什么需要3级Cache,将正在创建的bean直接放入二级缓存可以吗,为什么要先放到singletonFactories()?
/** Names of beans that are currently in creation (using a ConcurrentHashMap as a Set) */
Map<String, Boolean> singletonsCurrentlyInCreation
正在创建bean列表
-------------------------------------------------------abstractBeanFactory#getObjectForBeanInstance:start----------------------------------------------
abstractBeanFactory#getObjectForBeanInstance:-此方法判断bean是普通bean或者factoryBean
/** Cache of singleton objects created by FactoryBeans: FactoryBean name --> object */
Map<String, Object> factoryBeanObjectCache
该缓存可看着singletonObjects的子缓存,singletonObjects存的是factoryBean的实例,factoryBeanObjectCache存的是生成的对象getObject()的实例
singletonObjects是否存factory.getObject()产生的bean实例?
如果是普通bean实例直接返回,否则从factoryBean.getObject()获取,通过factoryBean.getOobject()的任务由abstractBeanFactory父类factoryBeanRegistrySupport来完成
factoryBeanRegistrySupport#getObjectFromFactoryBean
abstractoryBeanFactory每个父类都分工明确,这体现了java设计的单一职责原则。有时从类名称就可以看出其作用
-------------------------------------------------------abstractBeanFactory#getObjectForBeanInstance:end----------------------------------------------
2.缓存中没有实例,则开始创建
2.1 判断该bean是否正在创建
abstractBeanFactory
/** Names of beans that are currently in creation */
ThreadLocal<Object> prototypesCurrentlyInCreation
2.2 判断是否有父beanFactory并且在本次beanFactory中没有该beanDefinition
是不是父beanFactory可以定义同一名称的bean?
2.3 标记该bean至少创建了一次
/** Names of beans that have already been created at least once */
Map<String, Boolean> alreadyCreated
alreadyCreated添加数据
该语句作用不是很明确,以下为其注释
2.4 获取beanDefinition并创建
2.4.1 合成RootBeanDefinition
/** Map from bean name to merged RootBeanDefinition */
Map<String, RootBeanDefinition> mergedBeanDefinitions
在document到beanDefinition的过程中,创建的是GenericBeanDefinition的对象
mergedBeanDefinitions添加数据
2.4.2 是否存在前置依赖,存在则先创建前置对象
<bean ... depends-on="otherBeanName"/>
DefaultSingletonBeanRegistry
/** Map between dependent bean names: bean name --> Set of dependent bean names */
Map<String, Set<String>> dependentBeanMap
bean->前置bean
/** Map between depending bean names: bean name --> Set of bean names for the bean's dependencies */
Map<String, Set<String>> dependenciesForBeanMap
前置bean->bean
dependentBeanMap添加数据
dependenciesForBeanMap添加数据
这2个缓存有什么用?
2.4.3 Create bean instance
这里分三种情况(单例,原型,其他)
单例
abstractBeanFactory#doGetBean
这里也分2步,先创建实例,再判断是否是真实bean或者factoryBean返回
defaultSingletonBeanRegistry#getSingleton(模板方法)
下面是每个语句作用的简单描述:
/** List of suppressed Exceptions, available for associating related causes */
Set<Exception> suppressedExceptions
suppressedExceptions的数据来源是什么?
/** Flag that indicates whether we're currently within destroySingletons */
boolean singletonsCurrentlyInDestruction
/** Names of beans currently excluded from in creation checks (using a ConcurrentHashMap as a Set) */
Map<String, Boolean> inCreationCheckExclusions
beforeSingletonCreation(beanName);
singletonsCurrentlyInCreation添加数据
singletonObject = singletonFactory.getObject(); -- 下面点分析
afterSingletonCreation(beanName);
singletonsCurrentlyInCreation删除数据
addSingleton(beanName, singletonObject);
/** Set of registered singletons, containing the bean names in registration order */
Set<String> registeredSingletons
singletonObjects添加数据
singletonFactories删除数据
earlySingletonObjects删除数据
registeredSingletons添加数据
singletonFactory.getObject();
此时,创建实例的任务转移到了DefaultListableBeanFactory的父类AbstractAutowireCapableBeanFactory
DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
AbstractAutowireCapableBeanFactory extends AbstractBeanFactory
abstractBeanFactory
getBean()
-------------------------------------------------------父子类不同职责的分割线----------------------------------------------
abstractAutowireCapableBeanFactory
createBean()
abstractAutowireCapableBeanFactory#createBean
mbd.prepareMethodOverrides();
处理标签<bean ... replace-method="" lookup-method="">
abstractAutowireCapableBeanFactory源码:
http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.beans/3.2.2/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java/
abstractAutowireCapableBeanFactory#doCreateBean:
/** Cache of unfinished FactoryBean instances: FactoryBean name --> BeanWrapper */
Map<String, BeanWrapper> factoryBeanInstanceCache
4.1.2
何时处理这个属性externallyManagedConfigMembers?
// 4.1.3
singletonFactories添加数据
earlySingletonObjects删除数据
registeredSingletons添加数据
// 4.3
根据dependentBeanMap优化实例化
4.2.1
AbstractAutowireCapableBeanFactory#populateBean
4.2.1.1.2与4.2.1.1.3处理的属性值会不会重复?
4.2.2
AbstractAutowireCapableBeanFactory#initializeBean
4.2.2.3
处理标签<bean ... init-method="">
原型prototype
其它
request
session
global session
参考文章:
http://www.iteye.com/topic/1122859
http://yoyanda.blog.51cto.com/9675421/1721243
http://dba10g.blog.51cto.com/764602/1726519
http://m.blog.csdn.net/article/details?id=49283035
http://www.jianshu.com/p/6c359768b1dc
https://gavinzhang1.gitbooks.io/spring/content/zhun_bei_chuang_jian_bean.html
https://gavinzhang1.gitbooks.io/spring/content/chuang_jian_bean.html
BeanWrapper 设置和获取属性值
http://uule.iteye.com/blog/2105426
https://www.ibm.com/developerworks/cn/java/j-lo-beanannotation/
http://zhoujian1982318.iteye.com/blog/1696567
Spring中Autowired注解,Resource注解和xml default-autowire工作方式异同:
https://www.iflym.com/index.php/code/201211070001.html
bean的作用域 - Spring Framework reference 2.0.5 参考手册中文版
http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch03s04.html
从xml到document
从document到beanDefinition
从beanDefiniton到instance
本文核心包括2部分:
4.1.实例化
4.2.初始化
这部分可以看到bean设置属性值及执行方法的顺序
从beanDefinition到instance
从beanDefinition到instance的上下文入口,也是在
abstractApplicationContext.refresh:(abstractApplicationContext implements ConfigurableWebApplicationContext)
具体查看:
<spring源码学习系列2.1-从xml到document>
abstractApplicationContext#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(); //此处实例化bean // 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; } } }
其中
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
实例化beanDefinition定义的bean
abstractApplicationContext#finishBeanFactoryInitialization
/** * Finish the initialization of this context's bean factory, * initializing all remaining singleton beans. */ protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) { // Initialize conversion service for this context. if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) && beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) { beanFactory.setConversionService( beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)); } // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early. String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false); for (String weaverAwareName : weaverAwareNames) { getBean(weaverAwareName); } // Stop using the temporary ClassLoader for type matching. beanFactory.setTempClassLoader(null); // Allow for caching all bean definition metadata, not expecting further changes. beanFactory.freezeConfiguration(); // Instantiate all remaining (non-lazy-init) singletons. beanFactory.preInstantiateSingletons(); }
这里的CONVERSION_SERVICE_BEAN_NAME是何时定义的?
// Instantiate all remaining (non-lazy-init) singletons.
beanFactory.preInstantiateSingletons();
此处实例化操作委托给beanFactory-由此可见,applicationContext的核心还是beanFactory
applicationContext本身已经实现了ListableBeanFactory, HierarchicalBeanFactory等接口,为什么还要另在里面定义一个AbstractRefreshableApplicationContext.beanFactory(DefaultListableBeanFactory)?
其实跟踪代码可知,appContext实现了ListableBeanFactory, HierarchicalBeanFactory接口。最终也是调用getBeanFactory()来实现其功能。
这样一来可以为spring瘦身,不必把DefaultListableBeanFactory实现的功能在spring中,有写一遍
二来让spring看起来也像一个容器BeanFactory,只不过更高级点,实现了其他beanFactory没有的功能
applicationContext
-------------------------------------------------------承上启下的分割线----------------------------------------------
defaultListableBeanFactory
真正的实例化beanDefinition还是由beanFactory来实现
DefaultListableBeanFactory的源码:
http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-beans/3.2.7.RELEASE/org/springframework/beans/factory/support/DefaultListableBeanFactory.java/
创建RootBeanDefinition
defaultListableBeanFactory#preInstantiateSingletons
public void preInstantiateSingletons() throws BeansException { if (this.logger.isInfoEnabled()) { this.logger.info("Pre-instantiating singletons in " + this); } List<String> beanNames; synchronized (this.beanDefinitionMap) { // Iterate over a copy to allow for init methods which in turn register new bean definitions. // While this may not be part of the regular factory bootstrap, it does otherwise work fine. beanNames = new ArrayList<String>(this.beanDefinitionNames); } // Trigger initialization of all non-lazy singleton beans... for (String beanName : beanNames) { RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (isFactoryBean(beanName)) { final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName); boolean isEagerInit; if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() { public Boolean run() { return ((SmartFactoryBean<?>) factory).isEagerInit(); } }, getAccessControlContext()); } else { isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit()); } if (isEagerInit) { getBean(beanName); } } else { getBean(beanName); } } } }
这里开始循环实例化beanDefinition的bean,
首先判断是否是factoryBean,这里假设为非factoryBean,现实情况也是非factoryBean居多
DefaultListableBeanFactory开始将创建bean的任务给父类AbstractBeanFactory-spring的单一职责原则
defaultListableBeanFactory
创建rootBeanDefinition
-------------------------------------------------------父子类不同职责的分割线----------------------------------------------
abstractBeanFactory
getBean()
1.判断instance是否已经存在,存在则返回
2.获取RootBeanDefinition 委托AbstractAutowireCapableBeanFactory创建
abstractBeanFactory#getBean(String name):
public Object getBean(String name) throws BeansException { return doGetBean(name, null, null, false); }
abstractBeanFactory#doGetBean(final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
该方法代码量较大,读者可直接查看源代码,这里对方法做的事做分段简单描述:
1.判断该类是否已实例化// Eagerly check singleton cache for manually registered singletons.
abstractBeanFactory#doGetBean
// 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); }
这里又两部分操作:
一是判断是否正在生成或已经生成
二是根据上一步得到的是真正的bean instance还是factoryBean,返回真正的bean instance
defaultSingletonBeanRegistry#getSingleton:(AbstractBeanFactory继承了DefaultSingletonBeanRegistry)
protected Object getSingleton(String beanName, boolean allowEarlyReference) { Object singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) { synchronized (this.singletonObjects) { singletonObject = this.earlySingletonObjects.get(beanName); if (singletonObject == null && allowEarlyReference) { ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName); if (singletonFactory != null) { singletonObject = singletonFactory.getObject(); this.earlySingletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); } } } } return (singletonObject != NULL_OBJECT ? singletonObject : null); }
earlySingletonObjects添加数据
singletonFactories删除数据
这里有好几个重要的属性(数据结构):-注意观察何时添加数据何时
删除数据及存放什么类型的数据
doGetBean的过程就是相当于对这些数据结构就行操作,而向其添加删除修改数据的过程就是算法
this.singletonObjects换成普通的HashMap<String, Object>可以吗?
DefaultSingletonBeanRegistry
/** Cache of singleton objects: bean name --> bean instance */
Map<String, Object> singletonObjects
单例列表-一级缓存
/** Cache of singleton factories: bean name --> ObjectFactory */
Map<String, ObjectFactory<?>> singletonFactories
生成单例的工厂缓存-三级缓存
/** Cache of early singleton objects: bean name --> bean instance */
Map<String, Object> earlySingletonObjects
早期单例列表-二级缓存
这里的三级Cache是spring用来解决循环引用而设计的
参考:http://www.jianshu.com/p/6c359768b1dc
最早是添加3级缓存数据
addSingletonFactory(beanName, new ObjectFactory<Object>() {
@Override public Object getObject() throws BeansException {
return getEarlyBeanReference(beanName, mbd, bean);
}});
对于第一次生成实例而言,此处3个Cache都为空
为什么需要3级Cache,将正在创建的bean直接放入二级缓存可以吗,为什么要先放到singletonFactories()?
/** Names of beans that are currently in creation (using a ConcurrentHashMap as a Set) */
Map<String, Boolean> singletonsCurrentlyInCreation
正在创建bean列表
-------------------------------------------------------abstractBeanFactory#getObjectForBeanInstance:start----------------------------------------------
abstractBeanFactory#getObjectForBeanInstance:-此方法判断bean是普通bean或者factoryBean
/** Cache of singleton objects created by FactoryBeans: FactoryBean name --> object */
Map<String, Object> factoryBeanObjectCache
该缓存可看着singletonObjects的子缓存,singletonObjects存的是factoryBean的实例,factoryBeanObjectCache存的是生成的对象getObject()的实例
singletonObjects是否存factory.getObject()产生的bean实例?
如果是普通bean实例直接返回,否则从factoryBean.getObject()获取,通过factoryBean.getOobject()的任务由abstractBeanFactory父类factoryBeanRegistrySupport来完成
factoryBeanRegistrySupport#getObjectFromFactoryBean
abstractoryBeanFactory每个父类都分工明确,这体现了java设计的单一职责原则。有时从类名称就可以看出其作用
-------------------------------------------------------abstractBeanFactory#getObjectForBeanInstance:end----------------------------------------------
2.缓存中没有实例,则开始创建
2.1 判断该bean是否正在创建
abstractBeanFactory
/** Names of beans that are currently in creation */
ThreadLocal<Object> prototypesCurrentlyInCreation
2.2 判断是否有父beanFactory并且在本次beanFactory中没有该beanDefinition
是不是父beanFactory可以定义同一名称的bean?
2.3 标记该bean至少创建了一次
/** Names of beans that have already been created at least once */
Map<String, Boolean> alreadyCreated
alreadyCreated添加数据
该语句作用不是很明确,以下为其注释
引用
/**
* Mark the specified bean as already created (or about to be created).
* <p>This allows the bean factory to optimize its caching for repeated
* creation of the specified bean.
* @param beanName the name of the bean
*/
* Mark the specified bean as already created (or about to be created).
* <p>This allows the bean factory to optimize its caching for repeated
* creation of the specified bean.
* @param beanName the name of the bean
*/
2.4 获取beanDefinition并创建
2.4.1 合成RootBeanDefinition
/** Map from bean name to merged RootBeanDefinition */
Map<String, RootBeanDefinition> mergedBeanDefinitions
在document到beanDefinition的过程中,创建的是GenericBeanDefinition的对象
mergedBeanDefinitions添加数据
2.4.2 是否存在前置依赖,存在则先创建前置对象
<bean ... depends-on="otherBeanName"/>
DefaultSingletonBeanRegistry
/** Map between dependent bean names: bean name --> Set of dependent bean names */
Map<String, Set<String>> dependentBeanMap
bean->前置bean
/** Map between depending bean names: bean name --> Set of bean names for the bean's dependencies */
Map<String, Set<String>> dependenciesForBeanMap
前置bean->bean
dependentBeanMap添加数据
dependenciesForBeanMap添加数据
这2个缓存有什么用?
2.4.3 Create bean instance
这里分三种情况(单例,原型,其他)
单例
abstractBeanFactory#doGetBean
if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() { 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); }
这里也分2步,先创建实例,再判断是否是真实bean或者factoryBean返回
defaultSingletonBeanRegistry#getSingleton(模板方法)
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) { Assert.notNull(beanName, "'beanName' must not be null"); synchronized (this.singletonObjects) { Object singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null) { if (this.singletonsCurrentlyInDestruction) { throw new BeanCreationNotAllowedException(beanName, "Singleton bean creation not allowed while the singletons of this factory are in destruction " + "(Do not request a bean from a BeanFactory in a destroy method implementation!)"); } if (logger.isDebugEnabled()) { logger.debug("Creating shared instance of singleton bean '" + beanName + "'"); } beforeSingletonCreation(beanName); boolean recordSuppressedExceptions = (this.suppressedExceptions == null); if (recordSuppressedExceptions) { this.suppressedExceptions = new LinkedHashSet<Exception>(); } try { singletonObject = singletonFactory.getObject(); } catch (BeanCreationException ex) { if (recordSuppressedExceptions) { for (Exception suppressedException : this.suppressedExceptions) { ex.addRelatedCause(suppressedException); } } throw ex; } finally { if (recordSuppressedExceptions) { this.suppressedExceptions = null; } afterSingletonCreation(beanName); } addSingleton(beanName, singletonObject); } return (singletonObject != NULL_OBJECT ? singletonObject : null); } }
下面是每个语句作用的简单描述:
/** List of suppressed Exceptions, available for associating related causes */
Set<Exception> suppressedExceptions
suppressedExceptions的数据来源是什么?
/** Flag that indicates whether we're currently within destroySingletons */
boolean singletonsCurrentlyInDestruction
/** Names of beans currently excluded from in creation checks (using a ConcurrentHashMap as a Set) */
Map<String, Boolean> inCreationCheckExclusions
beforeSingletonCreation(beanName);
singletonsCurrentlyInCreation添加数据
singletonObject = singletonFactory.getObject(); -- 下面点分析
afterSingletonCreation(beanName);
singletonsCurrentlyInCreation删除数据
addSingleton(beanName, singletonObject);
/** Set of registered singletons, containing the bean names in registration order */
Set<String> registeredSingletons
singletonObjects添加数据
singletonFactories删除数据
earlySingletonObjects删除数据
registeredSingletons添加数据
singletonFactory.getObject();
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; } }
此时,创建实例的任务转移到了DefaultListableBeanFactory的父类AbstractAutowireCapableBeanFactory
DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
AbstractAutowireCapableBeanFactory extends AbstractBeanFactory
abstractBeanFactory
getBean()
-------------------------------------------------------父子类不同职责的分割线----------------------------------------------
abstractAutowireCapableBeanFactory
createBean()
abstractAutowireCapableBeanFactory#createBean
/** * Central method of this class: creates a bean instance, * populates the bean instance, applies post-processors, etc. * @see #doCreateBean */ @Override protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) throws BeanCreationException { if (logger.isDebugEnabled()) { logger.debug("Creating instance of bean '" + beanName + "'"); } // Make sure bean class is actually resolved at this point. // 1.设置beanDefinition的beanClass属性,如果没处理的话 resolveBeanClass(mbd, beanName); // Prepare method overrides. try { // 2.处理标签<bean ... replace-method="" lookup-method=""> mbd.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { // 3.BeanPostProcessor扩展点 ,会回调InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation 和 InstantiationAwareBeanPostProcessor#postProcessAfterInitialization。如果返回bean不为空,直接返回 //如:AbstractAutoProxyCreator#postProcessBeforeInstantiation 返回null,AbstractAutoProxyCreator#postProcessAfterInitialization并不会执行,而是初始化之后执行 // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. Object bean = resolveBeforeInstantiation(beanName, mbd); if (bean != null) { return bean; } } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed", ex); } // 4. 如果3过程没有生成代理对象,则实例化bean Object beanInstance = doCreateBean(beanName, mbd, args); if (logger.isDebugEnabled()) { logger.debug("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; }
mbd.prepareMethodOverrides();
处理标签<bean ... replace-method="" lookup-method="">
abstractAutowireCapableBeanFactory源码:
http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.beans/3.2.2/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java/
abstractAutowireCapableBeanFactory#doCreateBean:
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) { // 4.1 Instantiate the bean.-实例化 BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { // 4.1.1 实例化beanClass,并创建BeanWrapImpl(beanInstance) // BeanPostProcessor扩展点-回调SmartInstantiationAwareBeanPostProcessor#determineCandidateConstructors 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) { // 4.1.2 BeanPostProcessor扩展点-执行MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition,设置RootBeanDefinition的externallyManagedConfigMembers // 何时处理这个属性externallyManagedConfigMembers? // 实现MergedBeanDefinitionPostProcessor的后置处理器有AutowiredAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor 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"); } // 4.1.3 singletonFactories添加数据,earlySingletonObjects删除数据,registeredSingletons添加数据 addSingletonFactory(beanName, new ObjectFactory<Object>() { public Object getObject() throws BeansException { // BeanPostProcessor扩展点 回调SmartInstantiationAwareBeanPostProcessor#getEarlyBeanReference return getEarlyBeanReference(beanName, mbd, bean); } }); } // 4.2 Initialize the bean instance.-初始化 Object exposedObject = bean; try { // 4.2.1 设置beanInstance属性值 populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { // 4.2.2 执行beanInstance的初始化方法,如init() afterPropertiesSet() aware的接口方法还有bean post processors 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); } } // 4.3 优化 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."); } } } } // 4.4 // BeanPostProcessor扩展点-回调DestructionAwareBeanPostProcessor#postProcessBeforeDestruction //如InitDestroyAnnotationBeanPostProcessor完成@PreDestroy注解的销毁方法注册和调用 // Register bean as disposable. try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }
/** Cache of unfinished FactoryBean instances: FactoryBean name --> BeanWrapper */
Map<String, BeanWrapper> factoryBeanInstanceCache
4.1.2
何时处理这个属性externallyManagedConfigMembers?
// 4.1.3
singletonFactories添加数据
earlySingletonObjects删除数据
registeredSingletons添加数据
// 4.3
根据dependentBeanMap优化实例化
4.2.1
AbstractAutowireCapableBeanFactory#populateBean
protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) { // 4.2.1.1获取属性值 PropertyValues pvs = mbd.getPropertyValues(); if (bw == null) { if (!pvs.isEmpty()) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance"); } else { // Skip property population phase for null instance. return; } } // 4.2.1.1.1 BeanPostProcessor扩展点-改变特定实例的属性值,而非beanDefinition中定义的。回调InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation方法 // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the // state of the bean before properties are set. This can be used, for example, // to support styles of field injection. boolean continueWithPropertyPopulation = true; if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { continueWithPropertyPopulation = false; break; } } } } if (!continueWithPropertyPopulation) { return; } // 4.2.1.1.2 根据<beans default-autowire/> 自动添加符合添加的属性值 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); // Add property values based on autowire by name if applicable. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) { autowireByName(beanName, mbd, bw, newPvs); } // Add property values based on autowire by type if applicable. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { autowireByType(beanName, mbd, bw, newPvs); } pvs = newPvs; } boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors(); boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE); if (hasInstAwareBpps || needsDepCheck) { PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); // 4.2.1.1.3 BeanPostProcessor扩展点-根据@Autowired @Resource @Required @ PersistenceContext等注解,回调InstantiationAwareBeanPostProcessor#postProcessPropertyValues添加属性值。例如 // AutowiredAnnotationBeanPostProcessor#postProcessPropertyValues // CommonAnnotationBeanPostProcessor#postProcessPropertyValues // RequiredAnnotationBeanPostProcessor#postProcessPropertyValues // PersistenceAnnotationBeanPostProcessor#postProcessPropertyValues if (hasInstAwareBpps) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvs == null) { return; } } } } // 4.2.1.1.4 执行依赖检查 if (needsDepCheck) { checkDependencies(beanName, mbd, filteredPds, pvs); } } // 4.2.1.2 设置属性值到beanInstance-应用明确的setter属性注入 applyPropertyValues(beanName, mbd, bw, pvs); }
4.2.1.1.2与4.2.1.1.3处理的属性值会不会重复?
4.2.2
AbstractAutowireCapableBeanFactory#initializeBean
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) { // 4.2.2.1 执行BeanNameAware#setBeanName,BeanClassLoaderAware#setBeanClassLoader,BeanFactoryAware#setBeanFactory等接口方法 if (System.getSecurityManager() != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { invokeAwareMethods(beanName, bean); return null; } }, getAccessControlContext()); } else { invokeAwareMethods(beanName, bean); } // 4.2.2.2 BeanPostProcessor扩展点 回调BeanPostProcessor#postProcessBeforeInitialization,执行spring自定义的beanPostProcessor,例如: // ApplicationContextAwareProcessor 回调一些Aware接口,如EnvironmentAware,EmbeddedValueResolverAware, ResourceLoaderAware, ApplicationEventPublisherAware,MessageSourceAware,ApplicationContextAware // InitDestroyAnnotationBeanPostProcessor 调用有@PostConstruct注解的初始化方法。 // BeanValidationPostProcessor 处理@Valid Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } // 4.2.2.3 执行InitializingBean#afterPropertiesSet() 自定义的init-method try { invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } // 4.2.2.4 BeanPostProcessor扩展点 回调BeanPostProcessor#postProcessAfterInitialization // AspectJAwareAdvisorAutoProxyCreator(完成xml风格的AOP配置(<aop:config>)的目标对象包装到AOP代理对象) //AnnotationAwareAspectJAutoProxyCreator(完成@Aspectj注解风格(<aop:aspectj-autoproxy> @Aspect)的AOP配置的目标对象包装到AOP代理对象),其返回值将替代原始的Bean对象; //InfrastructureAdvisorAutoProxyCreator 处理<tx:transaction-annotation/> if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }
4.2.2.3
处理标签<bean ... init-method="">
原型prototype
其它
request
session
global session
参考文章:
http://www.iteye.com/topic/1122859
http://yoyanda.blog.51cto.com/9675421/1721243
http://dba10g.blog.51cto.com/764602/1726519
http://m.blog.csdn.net/article/details?id=49283035
http://www.jianshu.com/p/6c359768b1dc
https://gavinzhang1.gitbooks.io/spring/content/zhun_bei_chuang_jian_bean.html
https://gavinzhang1.gitbooks.io/spring/content/chuang_jian_bean.html
BeanWrapper 设置和获取属性值
http://uule.iteye.com/blog/2105426
https://www.ibm.com/developerworks/cn/java/j-lo-beanannotation/
http://zhoujian1982318.iteye.com/blog/1696567
Spring中Autowired注解,Resource注解和xml default-autowire工作方式异同:
https://www.iflym.com/index.php/code/201211070001.html
bean的作用域 - Spring Framework reference 2.0.5 参考手册中文版
http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch03s04.html
发表评论
-
spring疑难解惑-循环依赖的解决
2020-06-17 23:27 542AbstractAutowireCapableBeanFact ... -
spring容器
2019-07-14 08:47 305private final ServletContainer ... -
spring容器
2019-07-13 23:35 0spring容器与springmvc容器 73 ... -
spring源码学习系列2.6-spring ioc原理-codes
2019-03-05 22:56 488web.xml <listener> < ... -
spring源码学习系列3.4-spring mvc原理-codes
2019-01-21 22:46 297本篇章从核心类角度解读springmvc的原理 web.xm ... -
spring源码学习系列4.2-spring aop原理-codes
2018-12-04 22:29 562jdk: Proxy InvocationHandler ... -
spring源码学习系列4.1-spring实现对ibatis的事务管理
2018-09-17 15:44 579事务由spring管理,可以理解为由spring管理数据库连接 ... -
spring源码学习系列4-3种常用的自动代理创建器
2018-09-02 15:48 5713种自动代理器是 AnnotationAwareAspectJ ... -
spring源码学习系列1.2-spring事务代理深入分析2
2018-05-27 19:46 452提示: BeanPostProcessor AopUtils ... -
spring源码学习系列2.5-ApplicationContext初始化-设计模式
2018-05-08 15:17 521ApplicationContext容器的初始化可以通过模板方 ... -
spring源码学习系列3.3-DispatcherServlet初始化-设计模式
2018-05-07 11:12 623springmvc的核心是DispatcherServlet ... -
封装spring-security
2018-01-23 19:33 519github地址: https://github.com/ne ... -
eclipse导入spring源码
2018-05-12 07:20 982spring在git上的项目时gradle管理jar包的,所以 ... -
spring源码学习系列3.2.3-异常页面拦截机制
2017-07-29 19:07 768前序:本文的意义在于了解 tomcat处理异常 请求访问 ... -
spring源码学习系列3.2.2-How to bind String to Date
2017-07-17 12:40 599springmvc开发中,经常需将界面日期数据(String) ... -
spring源码学习系列3.2.1-command对象的绑定
2017-05-28 12:00 983在<spring源码学习系列3.2-handlerAda ... -
spring源码学习系列3.2-handlerAdapter执行
2017-05-28 12:01 410DispatcherServlet#doDispatch中调用 ... -
spring源码学习系列3.1-handlerMapping初始化
2017-05-28 11:56 703SimpleUrlHandlerMapping的继承体系 or ... -
spring源码学习系列2.4-finishRefresh会做什么
2017-05-06 16:36 579spring容器初始化完成后,调用finishRresh 该 ... -
spring源码学习系列3-springmvc原理
2017-05-28 11:56 458问题: springmvc是如何控 ...
相关推荐
在Spring框架中,"工厂Bean"是一个非常关键的概念,它扩展了传统的Bean定义,使得我们可以在Spring IoC容器中创建自定义的实例化逻辑。工厂Bean允许开发者在对象实例化时进行更复杂的控制,比如引入特定的初始化过程...
在Spring框架的开发和调试过程中,我们经常需要深入源码以理解其工作原理或解决特定问题。然而,直接编译Spring源码时,可能会遇到依赖缺失的问题。在本案例中,"Spring源码编译缺少的两个包:spring-cglib-repack-...
基于java的开发源码-Message-Driven Bean EJB实例源代码.zip 基于java的开发源码-Message-Driven Bean EJB实例源代码.zip 基于java的开发源码-Message-Driven Bean EJB实例源代码.zip 基于java的开发源码-Message-...
在本文中,我将讨论棘手的Spring Boot bean定义覆盖机制。 为了使您对该主题更加清楚,让我们从小测验开始。请看下一个简单的例子。 因此,我们有2种配置,它们使用名称beanName实例化bean,在主应用程序中,我们仅...
Spring 源码学习五:BeanDefinition 装载 1 在 Spring 框架中,BeanDefinition 是一个核心概念,它描述了一个 Bean 的定义,包括其依赖项、作用域、生命周期等信息。在本篇文章中,我们将深入探讨 Spring 的源码,...
Spring Ioc 源码分析系列--自动注入循环依赖的处理 本篇文章主要讲解了 Spring Ioc 源码分析系列中自动注入循环依赖的处理机制。循环依赖是 Spring 框架中一个经典的问题,也是面试中常被问到的问题。本篇文章将...
Spring Ioc源码分析系列--@Autowired注解的实现原理 @ Автоwired注解是 Spring Framework 中的一个重要组件,它提供了自动装配的功能,能够将 Bean 之间的依赖关系自动解析和注入。今天,我们将深入探讨 @...
通过对Spring 4.3.2.RELEASE源码的深入研究,我们可以了解其设计理念,学习到如何优雅地管理依赖、实现面向切面编程,以及如何利用Spring构建高效、健壮的Web应用。同时,源码阅读也能帮助我们理解Spring如何与其他...
在Spring框架中,BeanDefinition是核心组件之一,它定义了bean的基本信息和元数据,包括bean的类、...理解和掌握BeanDefinition对于深入学习Spring框架至关重要,因为它直接影响到bean的生命周期管理和依赖注入机制。
在学习源码时,应重点关注BeanDefinition的创建、加载和实例化过程,理解Spring如何将配置信息转化为运行时的对象。同时,理解BeanFactoryPostProcessor和BeanPostProcessor的作用,以及它们如何扩展Spring的功能,...
### Spring 源码分析——设计模式篇 #### 一、引言 Spring框架作为Java企业级开发领域中不可或缺的一部分,其内部集成了多种设计模式,不仅有助于提高系统的可维护性和扩展性,还能够帮助开发者更好地理解和应用...
在BeanFactory篇中,我们了解到BeanFactory在启动时会从配置元信息(通常是XML文件)中读取BeanDefinition,并将它们注册到BeanDefinitionRegistry中。这个过程是通过一系列的委托和解析机制完成的。 当我们运行...
springframework 是sping 里面的一个开源框架,主要用户javaee的企业开发。Spring是什么呢?首先它是一个开源的项目,而且非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层中必须...
Spring 源代码分析系列涵盖了多个关键模块,包括事务处理、IoC容器、JDBC、MVC、AOP以及与Hibernate和Acegi安全框架的集成。以下是对这些知识点的详细阐述: 1. **Spring 事务处理**:Spring 提供了声明式事务管理...
Spring Boot是Java开发中的一个关键框架,它简化了基于Spring的应用程序开发流程。Spring Boot 2.6.2是该框架的一个稳定版本,提供了一系列...对于Java开发者来说,研究Spring Boot源码是一项非常有价值的学习过程。
2. 控制反转:IoC是指应用程序的控制权从应用程序本身转移到框架,Spring容器根据配置文件动态地创建、装配和管理对象。这种设计模式使得组件之间解耦,降低了系统复杂度。 三、核心组件分析 1. ...
通过阅读源码,开发者可以理解Spring是如何通过精心设计的接口和类,以及一系列的设计模式,实现了高可扩展性和易用性的。 总的来说,Spring框架5.2.8.RELEASE源码的学习是一次深入理解Java企业级开发、掌握模块化...
通过阅读和分析Spring 1.2.6的源码,不仅可以学习到Spring的核心设计原则,还能了解到设计模式的运用,例如工厂模式、单例模式、观察者模式等。同时,这也是提升Java编程技巧和理解框架底层运作的好机会。在实际的...
Spring Boot是Java开发领域的一款非常流行的框架,它简化了基于Spring的应用程序开发流程。Spring Boot 2.6.2是该框架...通过分析源码,我们可以学习到Spring框架的最佳实践,以及如何设计和实现一个健壮的微服务架构。
mysql-connector-java-8.0.18,struts2.3我用的是mysql8所以文档配置的是MySQL8的加载驱动和url,如果你用的5,自己改一下就好了,相信你也知道在哪改,就是在applicatinContext.xml里的bean 中dataSource 和...