Dubbo源码学习ScopeBeanFactory对比Spring 的BeanFactory
Posted Dream_it_possible!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo源码学习ScopeBeanFactory对比Spring 的BeanFactory相关的知识,希望对你有一定的参考价值。
目录
1. ScopeBeanFactory与BeanFactory对比
1. ScopeBeanFactory与BeanFactory对比
ScopeBeanFactory是Dubbo自己定义的管理Bean的一个类, 他可与ScopeModel一起实现应用和模块之间的数据共享等功能,ScopeBeanFactory在ScopeModel类的initialize()方法里初始化, scopeModel 有3个实现,分别为ApplilcationMode、FrameWorkModel和ModuleMode, 其中FrameWorkModel可实现多应用之间的共享数据功能。
public abstract class ScopeModel implements ExtensionAccessor
protected static final Logger LOGGER = LoggerFactory.getLogger(ScopeModel.class);
protected void initialize()
this.extensionDirector = new ExtensionDirector(parent != null ? parent.getExtensionDirector() : null, scope, this);
this.extensionDirector.addExtensionPostProcessor(new ScopeModelAwareExtensionProcessor(this));
this.beanFactory = new ScopeBeanFactory(parent != null ? parent.getBeanFactory() : null, extensionDirector);
this.destroyListeners = new LinkedList<>();
this.classLoaderListeners = new LinkedList<>();
this.attributes = new ConcurrentHashMap<>();
this.classLoaders = new ConcurrentHashSet<>();
// Add Framework's ClassLoader by default
ClassLoader dubboClassLoader = ScopeModel.class.getClassLoader();
if (dubboClassLoader != null)
this.addClassLoader(dubboClassLoader);
//...
ScopeBeanFactory 管理Bean的方式类似于Spring BeanFactory, 不同的是Spring BeanFactory是一个接口,主要实现是DefaultListableBeanFactory。
ScopeBeanFactory和Spring 的BeanFactory,对比如下:
Dubbo | Spring | 其他 | |
描述Bean的方式 | BeanInfo对象 | BeanDefinition接口 | |
有无父亲 | 包含parent, 在本类里定义。 | 包含parent , BeanFactory, 在AbstractBeanFactory抽象类里定义。 | 他们在getBean时都先找自己的,自己没有再找父亲的, 判断的时候如果自己没有,那么直接返回false, 因为父亲那儿也时基于儿子的。 |
存放容器 | CopyOnWriteArrayList | ConcurrentHashMap | |
初始化Bean后的操作 | 执行initializeBean方法,然后执行所有的ExtensionPostProccess | Bean 初始化结束后在AbstractAutowireCapableBeanFactory类里applyBeanPostProceesorAfterInitialization(Object existingBean, String name) | |
PostProcessor | ExtensionAccessor接口提供了一系列的ExtensionPostProcessor | 在AbstractAutowireCapableBeanFactory抽象类里通过一个CopyOnWriteArrayList存放了所有的BeanPostProccessor | 注册Bean完成后执行 |
能限制Bean的作用域,跨应用共享 | 支持,通过选择不同的ScopeModel实现。 | 不支持 |
由上,我感觉ScopeBeanFactory就是一个简化版的BeanFactory,接着跟着代码一探究竟。
2. 注册Bean
注册Bean, 通过name 和对象可以直接注册Bean。
public void registerBean(Object bean)
this.registerBean(null, bean);
public void registerBean(String name, Object bean)
checkDestroyed();
// avoid duplicated register same bean
if (containsBean(name, bean))
return;
Class<?> beanClass = bean.getClass();
if (name == null)
name = beanClass.getName() + "#" + getNextId(beanClass);
initializeBean(name, bean);
registeredBeanInfos.add(new BeanInfo(name, bean));
this.getOrRegisterBean, 如果没有Bean,那么根据class创建Bean
public <T> T getOrRegisterBean(String name, Class<T> type)
T bean = getBean(name, type);
if (bean == null)
// lock by type
synchronized (type)
bean = getBean(name, type);
if (bean == null)
bean = createAndRegisterBean(name, type);
return bean;
也通过supplier传进来一个Bean, 执行apply 方法的时候回调。
public <T> T getOrRegisterBean(Class<T> type, Function<? super Class<T>, ? extends T> mappingFunction)
return getOrRegisterBean(null, type, mappingFunction);
public <T> T getOrRegisterBean(String name, Class<T> type, Function<? super Class<T>, ? extends T> mappingFunction)
T bean = getBean(name, type);
if (bean == null)
// lock by type
synchronized (type)
bean = getBean(name, type);
if (bean == null)
// 执行目标方法,得到bean对象。
bean = mappingFunction.apply(type);
registerBean(name, bean);
return bean;
3. 执行一系列的PostProccessor
注册Bean后的初始化,执行类似于Spring 的Bean的初始化动作:
执行一系列的PostProcessor, Spring里叫BeanPostProcessor, 这里只是换了个名字叫ExtensionPostProcessor。
private void initializeBean(String name, Object bean)
checkDestroyed();
try
if (bean instanceof ExtensionAccessorAware)
((ExtensionAccessorAware) bean).setExtensionAccessor(extensionAccessor);
// 执行postProcessAfterInitialization(bean,name), 执行一系列的BeanPostProccessor
for (ExtensionPostProcessor processor : extensionPostProcessors)
processor.postProcessAfterInitialization(bean, name);
catch (Exception e)
throw new ScopeBeanException("register bean failed! name=" + name + ", type=" + bean.getClass().getName(), e);
Spring 会在初始化完毕后,AbstractAutowireCapableBeanFactory实现了执行一系列的BeanPostProcessor
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd)
if (System.getSecurityManager() != null)
AccessController.doPrivileged(new PrivilegedAction<Object>()
public Object run()
AbstractAutowireCapableBeanFactory.this.invokeAwareMethods(beanName, bean);
return null;
, this.getAccessControlContext());
else
this.invokeAwareMethods(beanName, bean);
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic())
wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
try
this.invokeInitMethods(beanName, wrappedBean, mbd);
catch (Throwable var6)
throw new BeanCreationException(mbd != null ? mbd.getResourceDescription() : null, beanName, "Invocation of init method failed", var6);
// 执行postProceesorAfterInitialization
if (mbd == null || !mbd.isSynthetic())
wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
return wrappedBean;
进入到 applyBeanPostProcessorsAfterInitialization方法:
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException
Object result = existingBean;
Iterator i$ = this.getBeanPostProcessors().iterator();
do
if (!i$.hasNext())
return result;
// 执行postProcessor
BeanPostProcessor beanProcessor = (BeanPostProcessor)i$.next();
result = beanProcessor.postProcessAfterInitialization(result, beanName);
while(result != null);
return result;
看了代码后,我感觉Dubbo的ScopeBeanFactory的实现基本是借鉴于Spring 的BeanFactory的设计实现 ,更清晰明了啦。
以上是关于Dubbo源码学习ScopeBeanFactory对比Spring 的BeanFactory的主要内容,如果未能解决你的问题,请参考以下文章
Dubbo源码学习ScopeBeanFactory对比Spring 的BeanFactory