Inside-springframework-Loader-registerBeanPostProcessors
说明:将类型为BeanPostProcessor的bean实例化并保存在beanFactory的beanPocessors
既将AopProxyFactory类型的bean先实例。
★ public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext, DisposableBean {
… …
registerBeanPostProcessors();
… …
}
☆曲径通幽
/*****************段落3: registerBeanPostProcessors *****************/
(7注)
private void registerBeanPostProcessors() throws BeansException {
final int beanProcessorTargetCount = getBeanFactory().getBeanPostProcessorCount() +
1 +getBeanNamesForType(BeanPostProcessor.class, true, false).length; (8)
//beanFactory的beanPostProcessors.add(beanPostProcessor);
getBeanFactory().addBeanPostProcessor(new
BeanPostProcessorChecker(beanProcessorTargetCount));
getBeanFactory().addBeanPostProcessor(new BeanPostProcessorChecker
(beanProcessorTargetCount));
(11:实例化类型为BeanPostProcessor的bean)
Map beanProcessorMap = getBeansOfType(BeanPostProcessor.class, true, false);
}
(8注:从beanFacotry中找到类型为BeanPostProcessor的bean,并返回其beanName)
public String[] getBeanNamesForType(Class type, boolean includePrototypes,
boolean allowEagerInit) {
return getBeanFactory().getBeanNamesForType(type, includePrototypes,
allowEagerInit);(9)
}
}
★ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
implements ConfigurableListableBeanFactory, BeanDefinitionRegistry {
// registerBeanPostProcessors 时type为BeanPostProcessor
public String[] getBeanNamesForType(Class type, boolean includePrototypes, boolean
allowEagerInit) { (9注)
for (Iterator it = this.beanDefinitionNames.iterator(); it.hasNext();) {
String beanName = (String) it.next();
RootBeanDefinition mbd = getMergedBeanDefinition(beanName,
false); (10)
//判断是否为factoryBean吗
boolean isFactoryBean = isBeanClassMatch(beanName, mbd,
FactoryBean.class);
//如果bean类型为org.springframework.beans.factory.
//config.BeanPostProcessor
if ((includePrototypes || mbd.isSingleton()) && isTypeMatch(beanName, type))
{
result.add(beanName);
}
}
return StringUtils.toStringArray(result);
}
public BeanDefinition getBeanDefinition(String beanName) throws (10注)
NoSuchBeanDefinitionException {
BeanDefinition bd = (BeanDefinition)
this.beanDefinitionMap.get(beanName);
return bd;
}
(11注)
public Map getBeansOfType(Class type, boolean includePrototypes, boolean
allowEagerInit) throws BeansException {
String[] beanNames = getBeanNamesForType(type, includePrototypes,
allowEagerInit);
for (int i = 0; i < beanNames.length; i++) {
String beanName = beanNames[i];
result.put(beanName, getBean(beanName)); (12)
}
}
}
☆ 柳暗花明(创建bean)
/*****************段落4: getBean(beanName)*****************/
(12注)
★ public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements
ConfigurableBeanFactory {
public Object getBean(String name, Class requiredType, final Object[] args)
throws BeansException {
final String beanName = transformedBeanName(name);(13)
Object bean = null;
Object sharedInstance = getSingleton(beanName);(14)
if (sharedInstance != null) {
… …
}
else {
this.alreadyCreated.add(beanName);
final RootBeanDefinition mergedBeanDefinition =
getMergedBeanDefinition(beanName, false); (15)
if (mergedBeanDefinition.isSingleton()) {
sharedInstance = getSingleton(beanName, new ObjectFactory() {
public Object getObject() throws BeansException {
try {
return createBean(beanName, mergedBeanDefinition, args);(16)
}
catch (BeansException ex) {
destroySingleton(beanName);
throw ex;
}
}
});
bean = getObjectForBeanInstance(sharedInstance, name,
mergedBeanDefinition);
}
//如果bean不是singleton的,每次生成一个新的bean
else if (mergedBeanDefinition.isPrototype()) {
// It's a prototype -> create a new instance.
Object prototypeInstance = null;
try {
beforePrototypeCreation(beanName);
prototypeInstance = createBean(beanName, mergedBeanDefinition, args);
}
finally {
afterPrototypeCreation(beanName);
}
bean = getObjectForBeanInstance(prototypeInstance,
name, mergedBeanDefinition);
}
}
}
}
★ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory
implements AutowireCapableBeanFactory {
protected Object createBean(String beanName, RootBeanDefinition (16注)
mergedBeanDefinition, Object[] args)
throws BeanCreationException {
Class beanClass = resolveBeanClass(mergedBeanDefinition, beanName); (17)
if (beanClass != null && !mergedBeanDefinition.isSynthetic()) {
Object bean = applyBeanPostProcessorsBeforeInstantiation(beanClass, beanName);(18)
if (bean != null) {
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);(19)
return bean;
}
}
instanceWrapper = createBeanInstance(beanName, mergedBeanDefinition, args);(20)
//将生成bean存入beanFactory的singletonCache中
addSingleton(beanName, bean);
populateBean(beanName, mergedBeanDefinition, instanceWrapper); (29)
bean = initializeBean(beanName, bean, mergedBeanDefinition);
}
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition (20注)
mergedBeanDefinition, Object[] args) throws BeansException {
instanceWrapper = instantiateBean(beanName, mergedBeanDefinition);(21)
}
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition (21注)
mergedBeanDefinition) throws BeansException {
Object beanInstance = getInstantiationStrategy().instantiate(mergedBeanDefinition,
beanName, this);(22:根据BeanDefinition实例化bean)
BeanWrapper bw = new BeanWrapperImpl(beanInstance);(26:包装bean)
initBeanWrapper(bw);
return bw;
}
}
★ public class SimpleInstantiationStrategy implements InstantiationStrategy {
(22注)
public Object instantiate(
RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) {
// Don't override the class with CGLIB if no overrides.
if (beanDefinition.getMethodOverrides().isEmpty()) {
return BeanUtils.instantiateClass(beanDefinition.getBeanClass());(23)
}
else {
// Must generate CGLIB subclass.
return instantiateWithMethodInjection(beanDefinition, beanName, owner);
}
}
}
★ public abstract class BeanUtils {
public static Object instantiateClass(Class clazz) throws BeanInstantiationException {
return instantiateClass(clazz.getDeclaredConstructor((Class[]) null), null);(24)
}
public static Object instantiateClass(Constructor ctor, Object[] args) throws
BeanInstantiationException {
if (!Modifier.isPublic(ctor.getModifiers()) ||
!Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) {
ctor.setAccessible(true);
}
//实例化bean
return ctor.newInstance(args);(25)
}
}
★ public abstract class AbstractBeanDefinition extends AttributeAccessorSupport implements BeanDefinition {
(17注)
public Class resolveBeanClass(ClassLoader classLoader) throws ClassNotFoundException {
if (this.beanClass == null) {
return null;
}
//通过ClassLoader加载类
Class resolvedClass = ClassUtils.forName(getBeanClassName(), classLoader);
this.beanClass = resolvedClass;
return resolvedClass;
}
}