createBean方法在AbstractBeanFactory类中定义,具体实现在AbstractAutowireCapableBeanFactory类中实现,关于getBean方法的整体解析可以看这里getBean方法解析
protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
if (logger.isDebugEnabled()) {
logger.debug("Creating instance of bean '" + beanName + "'");
}
// 确保此时实际解析了bean类
//-----------------------------------------------方法1-----------------------------------------------//
resolveBeanClass(mbd, beanName);
//-----------------------------------------------方法1-----------------------------------------------//
// Prepare method overrides.
try {
mbd.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbd.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
}
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
//-----------------------------------------------方法2-----------------------------------------------//
//如果bean实现了实例化前处理器接口的,则需要在实例化之前调用这个方法
//bean的生命周期的中实现了InstantiationAwareBeanPostProcessor会在这里调用实现的postProcessBeforeInstantiation
Object bean = resolveBeforeInstantiation(beanName, mbd);
//-----------------------------------------------方法2-----------------------------------------------//
if (bean != null) {
return bean;
}
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"BeanPostProcessor before instantiation of bean failed", ex);
}
//-----------------------------------------------方法3-----------------------------------------------//
Object beanInstance = doCreateBean(beanName, mbd, args);
//-----------------------------------------------方法3-----------------------------------------------//
if (logger.isDebugEnabled()) {
logger.debug("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
-
1.方法1resolveBeanClass在AbstractBeanFactory中定义
protected Class<?> resolveBeanClass(final RootBeanDefinition mbd, String beanName, final Class<?>... typesToMatch)
throws CannotLoadBeanClassException {
try {
//如果传入的RootBeanDefinition已经解析过,就直接返回RootBeanDefinition对象中的beanCalss
if (mbd.hasBeanClass()) {
return mbd.getBeanClass();
}
//不管能不能获取系统安全性都去获取RootBeanDefinition对象中的class
if (System.getSecurityManager() != null) {
return AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() {
public Class<?> run() throws Exception {
return doResolveBeanClass(mbd, typesToMatch);
}
}, getAccessControlContext());
}
else {
return doResolveBeanClass(mbd, typesToMatch);
}
}
catch (PrivilegedActionException pae) {
ClassNotFoundException ex = (ClassNotFoundException) pae.getException();
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
}
catch (ClassNotFoundException ex) {
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
}
catch (LinkageError err) {
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), err);
}
}
-
2.方法2resolveBeforeInstantiation在AbstractAutowireCapableBeanFactory中定义
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
Object bean = null;
//beforeInstantiationResolved默认为false
if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
// Make sure bean class is actually resolved at this point.
//检查这个bean是不是存在beanClass,是不是不是合成的,是否实现了InstantiationAwareBeanPostProcessor(bean实例化之后但在设置显式属性或自动装配之前的处理)接口
if (mbd.hasBeanClass() && !mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
//--------------------------方法1-------------------------//
bean = applyBeanPostProcessorsBeforeInstantiation(mbd.getBeanClass(), beanName);
//--------------------------方法1-------------------------//
if (bean != null) {
//--------------------------方法2-------------------------//
//挨个调用postProcessAfterInitialization方法
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
//--------------------------方法2-------------------------//
}
}
mbd.beforeInstantiationResolved = (bean != null);
}
return bean;
}
//--------------------------方法1-------------------------//
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName)
throws BeansException {
//获取这个bean的所有BeanPostProcessor
for (BeanPostProcessor bp : getBeanPostProcessors()) {
//如果是InstantiationAwareBeanPostProcessor类型的,就调用实现的方法
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
//调用实现的postProcessBeforeInstantiation方法
Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
if (result != null) {
return result;
}
}
}
return null;
}
//--------------------------方法2-------------------------//
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
result = beanProcessor.postProcessAfterInitialization(result, beanName);
if (result == null) {
return result;
}
}
return result;
}
网友评论