阿里云开发者社区,千万开发者的选择
阿里云开发者社区,百万精品技术内容、千节免费系统课程、丰富的体验场景、活跃的社群活动、行业专家分享交流,欢迎点击【阅读原文】加入我们。
阿里妹导读
本文通过实际案例挖掘编码中遇到的循环依赖问题。
一、问题简述
// 在离线打标服务类中注入审批服务,在“申请打标”时调用
public class PortraitOfflineLabelingServiceImpl implements PortraitOfflineLabelingService {
private AuditService auditService;
/** 申请打标 */
Boolean applyOdpsOfflineLabeling(Request request);
/** 执行打标 */
Boolean executeOdpsOfflineLabeling(Request request);
}
// 审批服务类,定义了操作类型-审批回调服务的map映射
(rollbackFor = Throwable.class)
public class AuditServiceImpl implements AuditService,ApplicationContextAware,InitializingBean {
private Map<EntityOperation<?>, AuditCallback> auditCallbackMap = new HashMap<>();
public void afterPropertiesSet() throws Exception {
Map<EntityOperation<?>, AuditCallback> auditCallbackMap = new HashMap<>();
Collection<AuditCallback> beans = applicationContext.getBeansOfType(AuditCallback.class)
.values();
beans.forEach(auditCallback -> auditCallback.supportBizOperations()
.forEach(item -> auditCallbackMap.put(item, auditCallback)));
this.auditCallbackMap = auditCallbackMap;
}
}
// 在离线打标审批回调类中注入离线打标服务类,用于调用“执行打标”的服务
public class OdpsOfflineLabelingCallback implements AuditCallback {
private PortraitOfflineLabelingService offlineLabelingService;
public List<EntityOperation<?>> supportBizOperations() {
return Lists.newArrayList(OdpsOfflineLabelingOperation.CREATE_ODPS_OFFLINE_LABELING_INFO);
}
}
二、为什么会出现循环依赖报错?
2.1. Spring Bean加载过程
singletonObjects: 一级缓存,保存实例化&属性注入&初始化完成的bean实例。数据结构是bean名称->bean实例的映射。
earlySingletonObjects: 二级缓存,用于保存实例化完成,但为属性注入和初始化完成的bean实例。数据结构是bean名称->bean实例的映射。
singletonFactories: 三级缓存,用于保存bean创建工厂,以便于后面扩展有机会创建代理对象并放入二级缓存。数据结构是bean名称->bean创建工厂的映射。
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
// 尝试从一级缓存中获取已经初始化完成的bean实例(完全装载好的bean)
Object singletonObject = this.singletonObjects.get(beanName);
// 如果一级缓存中没有该实例
if (singletonObject == null && this.isSingletonCurrentlyInCreation(beanName)) {
// 跑去二级缓存中获取创建中的实例
singletonObject = this.earlySingletonObjects.get(beanName);
// 如果二级缓存中也没有该实例
if (singletonObject == null && allowEarlyReference) {
// 加锁
synchronized(this.singletonObjects) {
// 二次判断一级缓存和二级缓存中是否存在该实例(加锁时间差)
singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null) {
// 还是没有取到实例,尝试从三级缓存中获取创建该实例的工厂
ObjectFactory<?> singletonFactory = (ObjectFactory)this.singletonFactories.get(beanName);
if (singletonFactory != null) {
// 通过工厂获取该实例的单例
singletonObject = singletonFactory.getObject();
// 将获取的bean从三级缓存中移除,并且升级到二级缓存中
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
}
}
// 返回获取的单例bean
return singletonObject;
}
createBeanInstance: 实例化Bean,获得未被填充属性的原始Bean。
populateBean: 如果Bean有需要注入的属性,则进行属性填充,前提是需要填充的属性已经存在于Spring容器中,否则会先加载该属性再进行填充。如果有循环依赖,问题就是在这个过程中发生的。
initializeBean: 执行bean的初始化过程,包括执行前置方法->执行初始化->执行后置方法。
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
throws BeanCreationException {
// 1.实例化bean
// 封装被创建的Bean对象
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
// 获取实例化对象的类型
Object bean = instanceWrapper.getWrappedInstance();
Class<?> beanType = instanceWrapper.getWrappedClass();
if (beanType != NullBean.class) {
mbd.resolvedTargetType = beanType;
}
// 调用PostProcessor后置处理器对bean进行一些操作
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.postProcessed = true;
}
}
// 当允许提前暴露时,将实例化好的bean放进singletonFactories三级缓存,用来解决循环依赖导致的问题。
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
if (logger.isTraceEnabled()) {
logger.trace("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
Object exposedObject = bean;
try {
// 2.开始填充bean属性(依赖注入)
populateBean(beanName, mbd, instanceWrapper);
// 3.执行初始化方法(包括前后置的处理器)
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
catch (Throwable ex) {
if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
throw (BeanCreationException) ex;
}
else {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
}
}
// 当通过提早暴露解决循环依赖问题时,需要进行单例校验;这里也是本次报错的地方,后面会讲到为啥报错。
if (earlySingletonExposure) {
Object earlySingletonReference = getSingleton(beanName, false);
if (earlySingletonReference != null) {
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
for (String dependentBean : dependentBeans) {
if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
actualDependentBeans.add(dependentBean);
}
}
if (!actualDependentBeans.isEmpty()) {
throw new BeanCurrentlyInCreationException(beanName,
"Bean with name '" + beanName + "' has been injected into other beans [" +
StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
"] in its raw version as part of a circular reference, but has eventually been " +
"wrapped. This means that said other beans do not use the final version of the " +
"bean. This is often the result of over-eager type matching - consider using " +
"'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.");
}
}
}
}
try {
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}
return exposedObject;
}
2.2. 循环依赖情况下bean的加载过程
2.3. Spring三级缓存没有解决本次报错的原因
三、怎么解决本次报错
3.1. 从根源解决-去除循环依赖
// 拆分申请打标和执行打标到两个服务中,打破循环依赖
public class PortraitApplyOfflineLabelingServiceImpl implements PortraitApplyOfflineLabelingService {
private AuditService auditService;
/** 申请打标 */
Boolean applyOdpsOfflineLabeling(Request request);
}
public class PortraitExecuteOfflineLabelingServiceImpl implements PortraitExecuteOfflineLabelingService {
/** 执行打标 */
Boolean executeOdpsOfflineLabeling(Request request);
}
3.2. @Lazy
public class OdpsOfflineLabelingCallback implements AuditCallback {
// 在属性注入的时候增加懒加载的注解
private PortraitOfflineLabelingService offlineLabelingService;
public List<EntityOperation<?>> supportBizOperations() {
return Lists.newArrayList(OdpsOfflineLabelingOperation.CREATE_ODPS_OFFLINE_LABELING_INFO);
}
}
阿里云开发者社区,千万开发者的选择
阿里云开发者社区,百万精品技术内容、千节免费系统课程、丰富的体验场景、活跃的社群活动、行业专家分享交流,欢迎点击【阅读原文】加入我们。
文章引用微信公众号"阿里开发者",如有侵权,请联系管理员删除!