FactoryBeanRegistrySupport
FactoryBeanRegistrySupport抽象类继承了DefaultSingletonBeanRegistry类,增加了对FactoryBean的处理。
类结构
字段
// 缓存factoryBean的对应关系
private final Map factoryBeanObjectCache = new ConcurrentHashMap<>(16);
方法解析
getTypeForFactoryBean
调用factoryBean的getObjectType方法返回class类型
protected Class> getTypeForFactoryBean(final FactoryBean> factoryBean) {
try {
if (System.getSecurityManager() != null) {
return AccessController.doPrivileged((PrivilegedAction>)
factoryBean::getObjectType, getAccessControlContext());
}
else {
return factoryBean.getObjectType();//调用factoryBean的getObjectType方法返回class类型。
}
}
catch (Throwable ex) {
// Thrown from the FactoryBean's getObjectType implementation.
logger.info("FactoryBean threw exception from getObjectType, despite the contract saying " +
"that it should return null if the type of its object cannot be determined yet", ex);
return null;
}
}
getCachedObjectForFactoryBean
从缓存中通过制定的beanName获取FactoryBean
protected Object getCachedObjectForFactoryBean(String beanName) {
return this.factoryBeanObjectCache.get(beanName);
}
getObjectFromFactoryBean
protected Object getObjectFromFactoryBean(FactoryBean> factory, String beanName, boolean shouldPostProcess) {
//如果是单例,且已实例化
if (factory.isSingleton() && containsSingleton(beanName)) {
//对singletonObjects加锁
synchronized (getSingletonMutex()) {
Object object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
//缓存没有
object = doGetObjectFromFactoryBean(factory, beanName);
// Only post-process and store if not put there already during getObject() call above
// (e.g. because of circular reference processing triggered by custom getBean calls)
// 预防调用上面那个方法时,有对factoryBeanObjectCache设置,所以重新取
Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
if (alreadyThere != null) {
object = alreadyThere;
}
else {
if (shouldPostProcess) {//对该对象进行后置处理
// 当前正在创建的bean
if (isSingletonCurrentlyInCreation(beanName)) {
// Temporarily return non-post-processed object, not storing it yet..
return object;
}
beforeSingletonCreation(beanName);
try {
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName,
"Post-processing of FactoryBean's singleton object failed", ex);
}
finally {
afterSingletonCreation(beanName);
}
}
if (containsSingleton(beanName)) {
//放入缓存
this.factoryBeanObjectCache.put(beanName, object);
}
}
}
//缓存有直接返回
return object;
}
}
else {
// 多例或者没获取过,直接获取
Object object = doGetObjectFromFactoryBean(factory, beanName);
if (shouldPostProcess) {
try {
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
}
}
return object;
}
}
doGetObjectFromFactoryBean
调用factoryBean的getObjectType方法返回对象
private Object doGetObjectFromFactoryBean(final FactoryBean> factory, final String beanName)
throws BeanCreationException {
Object object;
try {
if (System.getSecurityManager() != null) {
AccessControlContext acc = getAccessControlContext();
try {
object = AccessController.doPrivileged((PrivilegedExceptionAction
postProcessObjectFromFactoryBean
预留钩子
protected Object postProcessObjectFromFactoryBean(Object object, String beanName) throws BeansException {
return object;
}
getFactoryBean
返回FactoryBean
protected FactoryBean> getFactoryBean(String beanName, Object beanInstance) throws BeansException {
if (!(beanInstance instanceof FactoryBean)) {
throw new BeanCreationException(beanName,
"Bean instance of type [" + beanInstance.getClass() + "] is not a FactoryBean");
}
return (FactoryBean>) beanInstance;
}
removeSingleton
除了移除DefaultSingletonBeanRegistry的对应beanName几个缓存,还要移除factoryBeanObjectCache的缓存
protected void removeSingleton(String beanName) {
synchronized (getSingletonMutex()) {
super.removeSingleton(beanName);
this.factoryBeanObjectCache.remove(beanName);
}
}
clearSingletonCache
除了移除DefaultSingletonBeanRegistry的几个缓存,还要移除factoryBeanObjectCache的缓存
protected void clearSingletonCache() {
synchronized (getSingletonMutex()) {
super.clearSingletonCache();
this.factoryBeanObjectCache.clear();
}
}
getAccessControlContext
获取安全作用域
protected AccessControlContext getAccessControlContext() {
return AccessController.getContext();
}