前言
AbstractBeanFactor实现了ConfigurableBeanFactory接口。一棵树有不仅有主干,树干,还有花花叶叶。其他接口与实现是BeanFactory的主干与树干的话,那ConfigurableBeanFactory负责管理BeanFactory的花花叶叶。
void setParentBeanFactory(BeanFactory parentBeanFactory) throws IllegalStateException; void setBeanClassLoader(ClassLoader beanClassLoader); ClassLoader getBeanClassLoader(); void setTempClassLoader(ClassLoader tempClassLoader); ClassLoader getTempClassLoader(); void setCacheBeanMetadata(boolean cacheBeanMetadata); boolean isCacheBeanMetadata(); void setBeanExpressionResolver(BeanExpressionResolver resolver); BeanExpressionResolver getBeanExpressionResolver(); void setConversionService(ConversionService conversionService); ConversionService getConversionService(); void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar); void registerCustomEditor(Class requiredType, Class propertyEditorClass); void copyRegisteredEditorsTo(PropertyEditorRegistry registry); void setTypeConverter(TypeConverter typeConverter); TypeConverter getTypeConverter(); void addEmbeddedValueResolver(StringValueResolver valueResolver); boolean hasEmbeddedValueResolver(); String resolveEmbeddedValue(String value); void addBeanPostProcessor(BeanPostProcessor beanPostProcessor); int getBeanPostProcessorCount(); void registerScope(String scopeName, Scope scope); String[] getRegisteredScopeNames(); Scope getRegisteredScope(String scopeName); AccessControlContext getAccessControlContext();
void copyConfigurationFrom(ConfigurableBeanFactory otherFactory); void registerAlias(String beanName, String alias) throws BeanDefinitionStoreException; void resolveAliases(StringValueResolver valueResolver); BeanDefinition getMergedBeanDefinition(String beanName) throws NoSuchBeanDefinitionException; boolean isFactoryBean(String name) throws NoSuchBeanDefinitionException; void setCurrentlyInCreation(String beanName, boolean inCreation); boolean isCurrentlyInCreation(String beanName); void registerDependentBean(String beanName, String dependentBeanName); String[] getDependentBeans(String beanName); String[] getDependenciesForBean(String beanName);
关于parentBeanFactory 变量相关信息
void setParentBeanFactory(BeanFactory parentBeanFactory) throws IllegalStateException;
private BeanFactory parentBeanFactory;
调用方
- org.springframework.beans.factory.support.AbstractBeanFactory
- containsBean(String)
- doGetBean(String, Class<T>, Object[], boolean)
- getAliases(String)
- getMergedBeanDefinition(String, BeanDefinition, BeanDefinition)
- getMergedBeanDefinition(String)
- getType(String)
- isFactoryBean(String)
- isPrototype(String)
- isSingleton(String)
- isTypeMatch(String, ResolvableType)
- org.springframework.beans.factory.support.DefaultListableBeanFactory
- checkBeanNotOfRequiredType(Class<?>, DependencyDescriptor)
- getBean(Class<T>, Object...)
- isAutowireCandidate(String, DependencyDescriptor, AutowireCandidateResolver)
- isPrimary(String, Object)
- resolveNamedBean(Class<T>)
- toString()
从被调用的方法名可以方法,parentBeanFactory属性主要是在得到或创建bean的时候,需要进行同样的操作。比如getBean方法,在当前beanFactory没有得到bean,就会去parentBeanFactory执行getBean(){去操作同样的方法}。
publicT getBean(Class requiredType, Object... args) throws BeansException { NamedBeanHolder namedBean = resolveNamedBean(requiredType, args); if (namedBean != null) { return namedBean.getBeanInstance(); } BeanFactory parent = getParentBeanFactory(); if (parent != null) { return parent.getBean(requiredType, args); } throw new NoSuchBeanDefinitionException(requiredType); }
解读
parentBeanFactory的设计主要是解决在复杂环境的下,当前容器有parent容器的情况。但是这种设计在当前互联网与分布式应用中无法应用到,所以是一个。菜鸟啊个人,感觉这个功能已经十分过时了。
关于ClassLoader相关变量
void setBeanClassLoader(ClassLoader beanClassLoader);private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();public static ClassLoader getDefaultClassLoader() { ClassLoader cl = null; try { cl = Thread.currentThread().getContextClassLoader(); } catch (Throwable ex) { // Cannot access thread context ClassLoader - falling back... } if (cl == null) { // No thread context class loader -> use class loader of this class. cl = ClassUtils.class.getClassLoader(); if (cl == null) { // getClassLoader() returning null indicates the bootstrap ClassLoader try { cl = ClassLoader.getSystemClassLoader(); } catch (Throwable ex) { // Cannot access system ClassLoader - oh well, maybe the caller can live with null... } } } return cl; }
org.springframework.aop.aspectj org.springframework.aop.aspectj.annotation org.springframework.aop.config org.springframework.aop.scope org.springframework.beans.factory.support org.springframework.context.annotation org.springframework.context.event org.springframework.context.expression org.springframework.context.support org.springframework.remoting.jaxws
解读
- 可能会获得三种不同的类加载器
- 在不同应用中,spring的类加载器,可能不其他的类加载器 不一样。比如在tomcat容器中,获得的类加载器与spring默认类加载器不一样
- 不建议修改beanClassLoader类加载器的值
conversionService 变量
private ConversionService conversionService;public void setConversionService(ConversionService conversionService) { this.conversionService = conversionService;}@Overridepublic ConversionService getConversionService() { return this.conversionService;}}
String CONVERSION_SERVICE_BEAN_NAME = "conversionService";protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) { 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)); } .....}
解读
- 如果没有配置 ConversionService 子类,默认在容器里面是没有ConversionService实现类的getConversionService返回null
- 关于 ConversionService 的详解请求
关于BeanPostProcessor相关的变量
private final ListbeanPostProcessors = new ArrayList ();/** Indicates whether any InstantiationAwareBeanPostProcessors have been registered */private boolean hasInstantiationAwareBeanPostProcessors;/** Indicates whether any DestructionAwareBeanPostProcessors have been registered */private boolean hasDestructionAwareBeanPostProcessors;public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) { Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null"); this.beanPostProcessors.remove(beanPostProcessor); this.beanPostProcessors.add(beanPostProcessor); if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) { this.hasInstantiationAwareBeanPostProcessors = true; } if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) { this.hasDestructionAwareBeanPostProcessors = true; }}public int getBeanPostProcessorCount() { return this.beanPostProcessors.size();}public List getBeanPostProcessors() { return this.beanPostProcessors;}protected boolean hasInstantiationAwareBeanPostProcessors() { return this.hasInstantiationAwareBeanPostProcessors;}protected boolean hasDestructionAwareBeanPostProcessors() { return this.hasDestructionAwareBeanPostProcessors;}protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) { Object exposedObject = bean; if (bean != null && !mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof SmartInstantiationAwareBeanPostProcessor) { SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp; exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName); if (exposedObject == null) { return null; } } } } return exposedObject;}
解读
- beanPostProcessors变量保存了所有BeanPostProcessor实现类,
- hasDestructionAwareBeanPostProcessors与hasDestructionAwareBeanPostProcessors的作用是避免进行无效的循环操作
RootBeanDefinition相关变量
private boolean cacheBeanMetadata = true; protected RootBeanDefinition getMergedBeanDefinition( String beanName, BeanDefinition bd, BeanDefinition containingBd)throws BeanDefinitionStoreException { if (containingBd == null && isCacheBeanMetadata()) { this.mergedBeanDefinitions.put(beanName, mbd); } } private final MapmergedBeanDefinitions = new ConcurrentHashMap (256); private final Set alreadyCreated = Collections.newSetFromMap(new ConcurrentHashMap (256)); protected void markBeanAsCreated(String beanName) { if (!this.alreadyCreated.contains(beanName)) { synchronized (this.mergedBeanDefinitions) { if (!this.alreadyCreated.contains(beanName)) { clearMergedBeanDefinition(beanName); this.alreadyCreated.add(beanName); } } } } protected T doGetBean(final String name, final Class requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { if (!typeCheckOnly) { markBeanAsCreated(beanName); } try { final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); } }
解读
- 不建议修改cacheBeanMetadata的值
- mergedBeanDefinitions保存了RootBeanDefinition对象,RootBeanDefinition对象才会生成一个对象。其他的beanDefinition实现不会。奇怪的设计。
- alreadyCreated控制RootBeanDefinition并发创建问题
总结
本节的内容感觉比较惨白无力,有些细节点还是比较重要的。比如RootBeanDefinition,ConversionService,ClassLoad等。其他方面鸟菜啊,感觉就是无用就不解读了。