博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第三章 spring-bean之AbstractBeanFactory(5)
阅读量:6307 次
发布时间:2019-06-22

本文共 9219 字,大约阅读时间需要 30 分钟。

hot3.png

前言

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(){去操作同样的方法}。

public 
T 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

解读

  1. 可能会获得三种不同的类加载器
  2. 在不同应用中,spring的类加载器,可能不其他的类加载器 不一样。比如在tomcat容器中,获得的类加载器与spring默认类加载器不一样
  3. 不建议修改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));	}	.....}

解读

  1. 如果没有配置 ConversionService 子类,默认在容器里面是没有ConversionService实现类的getConversionService返回null
  2. 关于 ConversionService 的详解请求

关于BeanPostProcessor相关的变量

private final List
beanPostProcessors = 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;}

解读

  1. beanPostProcessors变量保存了所有BeanPostProcessor实现类,
  2. 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 Map
mergedBeanDefinitions = 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); } }

解读

  1. 不建议修改cacheBeanMetadata的值
  2. mergedBeanDefinitions保存了RootBeanDefinition对象,RootBeanDefinition对象才会生成一个对象。其他的beanDefinition实现不会。奇怪的设计。
  3. alreadyCreated控制RootBeanDefinition并发创建问题

总结

本节的内容感觉比较惨白无力,有些细节点还是比较重要的。比如RootBeanDefinition,ConversionService,ClassLoad等。其他方面鸟菜啊,感觉就是无用就不解读了。

转载于:https://my.oschina.net/u/1261452/blog/1922667

你可能感兴趣的文章
【工具】系统性能查看工具 dstat
查看>>
基于zepto或jquery的手机端弹出框成功,失败,加载特效
查看>>
php引用(&)
查看>>
Delphi 操作Flash D7~XE10都有 导入Activex控件 shockwave
查看>>
基于上下文无关文法的句子生成算法
查看>>
回顾两年前整理的前端规范
查看>>
你可能不知道的 css tricks
查看>>
服务网格内部的 VirtualService 和 DestinationRule 配置深度解析
查看>>
我的友情链接
查看>>
SEO工作之友好引导(二)
查看>>
ifcfg/ip/ss命令详解
查看>>
关于 Flume NG
查看>>
北电交换机常用配置
查看>>
Linux磁盘及文件系统管理
查看>>
Linux系统下Apache日志文件设置、更改默认网站目录、防止php***跨站设置、禁止空主机头...
查看>>
shell判断文件是否存在
查看>>
EXCHANGE事务日志和邮箱数据库的存储位置
查看>>
oracle conn /as sysdba后显示 乱码”???“
查看>>
(三)把域服务升级和迁移到Windows Server 2012 R2上
查看>>
动态主机配置协议:DHCP简介
查看>>