Spring源码深度解析
容器的基本实现
核心类介绍(org.springframework.beans)
DefaultlistableBeanFactory
是整个bean加载的核心部分,是Spring注册及加载bean的默认实现;
XmlBeanDefinitionReader
Bean的基本容器XmlBeanFactory
- (配置文件封装)使用Resource接口对配置文件进行封装;
- (加载Bean:1、获取对XML文件的验证模式;2、加载XML获得Document;3、根据Document注册Bean信息)XmlBeanFactory中 XmlBeanDefinitionReader 类型的 read 属性提供的方法 this.reader. loadBeanDefinitions(resource)
- (获取对XML的验证模式【DTD或XSD】以及对验证模式的读取)
getValidationModeForResource(resource)
detectValidationMode(resource)
this.validationModeDetector.detectValidationMode(inputStream);
- 获取Document
//委托给DefaultDocumentLoader
@Override
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
if (logger.isTraceEnabled()) {
logger.trace("Using JAXP provider [" + factory.getClass().getName() + "]");
}
DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
return builder.parse(inputSource);
}
- 解析并注册BeanDefinition
//XmlBeanDefinitionReader
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
try {
Document doc = doLoadDocument(inputSource, resource);
int count = registerBeanDefinitions(doc, resource);
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + count + " bean definitions from " + resource);
}
return count;
}
//.......
}
默认标签的解析(省略)
bean标签的解析及注册
alias标签的解析
import标签的解析
嵌入式beans标签的解析
自定义标签的解析(省略)
自定义标签使用
自定义标签解析
bean的加载
- 转换对应beanName
- 尝试从缓存中加载单例
- bean的实例化
- 原型模式的依赖检查
- 检测parentBeanFactory
- 将存储XML的配置文件的GernericBeanDefinition转换为RootBeanDefinition
- 寻找依赖
- 针对不同的scope进行bean的创建
- 类型转换
//org\springframework\beans\factory\support\AbstractBeanFactory.java
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
//1、转换对应beanName
final String beanName = transformedBeanName(name);
Object bean;
/*2、检测缓存或实例工厂是否有对应的实例
* 因为在创建单例bean的时候会存在依赖注入的情况,而在创建依赖的时候避免循环依赖
*Spring创建bean的原则是不等bean创建完成就会创建bean的Object提早曝光,也就是将ObjectFactory加入到缓存中,一旦下一个bean创建的时候需要依赖上个bean就直接使用ObjectFactory
*/
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
if (logger.isTraceEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
}
}
//3、返回对应的实例
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// 4、原型模式依赖检查
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// 5、缓存没有数据,去父工厂尝试 递归调用getBean()
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// Not found -> check parent.
String nameToLookup = originalBeanName(name);
if (parentBeanFactory instanceof AbstractBeanFactory) {
return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
nameToLookup, requiredType, args, typeCheckOnly);
}
else if (args != null) {
// Delegation to parent with explicit args.
return (T) parentBeanFactory.getBean(nameToLookup, args);
}
else if (requiredType != null) {
// No args -> delegate to standard getBean method.
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
else {
return (T) parentBeanFactory.getBean(nameToLookup);
}
}
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
try {
//6、将存储XML的配置文件的GernericBeanDefinition转换为RootBeanDefinition(后续的操作都是针对RootBeanDefinition)
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// 7、寻找依赖,初始化一个bean加载其依赖项
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dep : dependsOn) {
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
registerDependentBean(dep, beanName);
try {
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
// 8、根据不同的scope创造bean
if (mbd.isSingleton()) {
//8-1创建单例bean
sharedInstance = getSingleton(beanName, () -> {
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, () -> {
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.
//9、检查需要的类型是否符合bean的类型
if (requiredType != null && !requiredType.isInstance(bean)) {
try {
T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
if (convertedBean == null) {
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
return convertedBean;
}
catch (TypeMismatchException ex) {
if (logger.isTraceEnabled()) {
logger.trace("Failed to convert bean '" + name + "' to required type '" +
ClassUtils.getQualifiedName(requiredType) + "'", ex);
}
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
}
return (T) bean;
}
FactoryBean的使用
可以让我们自定义Bean的创建过程
缓存中获取单例bean
@Nullable
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;
}
从bean的实例中获取对象
- getObjectForBeanInstance方法的主要工作:
- 对FactoryBean做正确性的验证
- 对非FactoryBean不做任何处理
- 对bean进行转换
- 将从Factory中解析bean的工作委托给getObjectFromFactoryBean方法
获取单例
参考代码中8-1
Spring 中使用 getSingleton的重载方法 bean 的加载过程;
准备创建bean
- 根据设置的class属性或根据className来解析Class。
- 对override属性进行标记及验证。
- 应用初始化前的后处理器,解析指定bean是否存在初始化前的短路操作。
- 创建bean。
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
if (logger.isTraceEnabled()) {
logger.trace("Creating instance of bean '" + beanName + "'");
}
RootBeanDefinition mbdToUse = mbd;
//锁定class,根据class的属性或根据className来解析Class
Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
mbdToUse = new RootBeanDefinition(mbd);
mbdToUse.setBeanClass(resolvedClass);
}
// Prepare method overrides.
//校验及准备覆盖的方法
try {
mbdToUse.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
}
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
//给BeanPostProcessors一个机会来返回代理来替代真正的实例
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
catch (Throwable ex) {
throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
"BeanPostProcessor before instantiation of bean failed", ex);
}
try {
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
if (logger.isTraceEnabled()) {
logger.trace("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
// A previously detected exception with proper bean creation context already,
// or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
throw ex;
}
catch (Throwable ex) {
throw new BeanCreationException(
mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
}
}
循环依赖
Spring是如何解决循环依赖的?(构造器循环依赖/setter循环依赖)
- 构造器循环依赖:无法解决 ,抛出BeanCurrentlyInCreationException异常;
- setter注入方式循环依赖:Spring容器提前暴露刚完成构造器注入但未完成其他步骤的bean来完成的,只能解决单例作用域的bean依赖问题;通过提前暴露一个单例工厂方法,从而使其他的bean能够引用到该bean;
- prototype作用域的bean的循环依赖:Spring容器无法完成依赖注入,因为Spring容器不进行缓存该作用域的bean,因此无法提前暴露一个创建中的bean;
- singleton作用域bean 可以通过“setAllowCitularRefences(false);”来禁用循环引用;
创建bean
- 步骤(常规bean的创建过程是在doCreateBean()中完成的):
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
//1、如果是单例,清除缓存
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
//2、将BeanDefinition转化为BeanWrapper (bean包装器)
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
final Object bean = instanceWrapper.getWrappedInstance();
Class<?> beanType = instanceWrapper.getWrappedClass();
if (beanType != NullBean.class) {
mbd.resolvedTargetType = beanType;
}
// Allow post-processors to modify the merged bean definition.
//3、应用MergedBeanDefinitionPostProcessors(bean合并后的处理,Autowired注解就是通过此方法实现诸如类型的预解析)
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.postProcessed = true;
}
}
// Eagerly cache singletons to be able to resolve circular references
// even when triggered by lifecycle interfaces like BeanFactoryAware.
//4、是否需要提前曝光,进行循环依赖处理
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
if (logger.isTraceEnabled()) {
logger.trace("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
//4-1、当bean初始化完成之前,涉及到自动注入另一个类的时候,不会去直接直接去创建A,而是把A放到缓存的ObjectFactory中;
//4-2、(AOP动态织入就是在getEarlyBeanReference中)
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
// Initialize the bean instance.
Object exposedObject = bean;
try {
//5、对bean进行属性填充
populateBean(beanName, mbd, instanceWrapper);
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);
//6、进行循环依赖检查,只有在检测到有依赖循环的时候, earlySingletonReference不为空
if (earlySingletonReference != null) {
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
for (String dependentBean : dependentBeans) {
if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
actualDependentBeans.add(dependentBean);
}
}
//actualDependentBeans不为空表示存在循环依赖,对于prototype的bean进行抛出异常
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 " +
"'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.");
}
}
}
}
// 7、注册DisposableBean,便于销毁时调用
try {
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}
//8、创建完成并返回
return exposedObject;
}
创建bean的实例
- createBeanInstance方法
- autowireConstructor方法 实例化分为通用和带参,带参要面临大量的参数判断
- 实例化策略:通过判断beanDefinition.hasMethodOverrides()是否有要覆盖的方法,如果有使用CGLI,如果没有,则使用反射创建;
记录创建bean的ObjectFactory
循环依赖的解决思路:在B中创建依赖A时,通过ObjectFactory提供的实例化方法来中断A中的属性填充,使B中持有的A仅仅是刚刚初始化并没有填充任何属性的A,而这正式初始化A的步骤还是在最开始创建A的时候进行的,但是因为A与B中的A所表示的属性地址是一样的,所以在A中创建好的属性填充自然可以通过B中的A获取,这样就解决了循环依赖的问题;
属性注入
- populateBean()最主要的是属性填充;
- autowireByName()getBean
- autowireByType
- applyPropertyValues
初始化bean
- initializeBean() :已经实例化,且进行了属性的填充,这时将调用用户设定的初始化方法;
- invokeAwareMethods():对特殊的bean进行处理 【链接】Aware接口作用
- applyBeanPostProcessorsBeforeInitialization() : 应用后处理器
- invokeInitMethods() : 激活用户自定义的init()方法
- applyBeanPostProcessorsAfterInitialization(): 后处理器应用
注册DisposableBean
- 销毁方法的扩展入口
- 配置destroy-method或注册DestructionBeanPostProcessor来统一处理bean的销毁方法;
参考书籍《Spring源码深度解析》第二版