• Spring 随笔 AOP 3-自调用


    0. 最近的外卖多少会撒,好 好 好

    Spring事务处理时自我调用的解决方案及一些实现方式的风险


    • 依赖循环的场景下会出现自调用问题(aop代理没有注入,实际注入真实的bean实例,比如事务)的时候,往往是因为循环依赖的副作用,本文旨在分析"咋搞的"

    • 通常说的自调用,一般没有依赖循环的前置条件,通过BeanPostProcessor注入代理实例可以轻松拿下(本文所描述的场景并不能直接拿下)

    1. 先做个引导:为什么需要自调用

    AOP拦截器除了执行增强的前置、后置、环绕通知方法以外,在调用我们预设的、需要被增强的方法时,并不是调用代理对象的,而是我们自己写的、真实的、交给springbean池管理的实例(通过Class反射调用)。

    因此,我们在处理AOP方法在自身类里面嵌套的时候,就自然而然地会想到去自动获取当前类的代理实例来解决这个问题。

    2. 没有循环依赖的时候,自调用是怎么正常运转的

    我们从IOC过程中接入这个话题

    	// org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
    	protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
    			throws BeanCreationException {
    
    		// Instantiate the 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;
    		}
    
    		// Allow post-processors to modify the merged bean definition.
    		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;
    			}
    		}
    
    		// Eagerly cache singletons to be able to resolve circular references
    		// even when triggered by lifecycle interfaces like BeanFactoryAware.
    		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");
    			}
    			// 这里是伏笔
    			// 缓存当前bean的代理类工厂(三级缓存使用)
    			// 方法入参中的bean为未完成初始化的bean真实实例
    			addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
    		}
    
    		// Initialize the bean instance.
    		Object exposedObject = bean;
    		try {
    			// 属性注入
    			populateBean(beanName, mbd, instanceWrapper);
    			// step into ...
    			// 属性注入后执行bean的初始化方法
    			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.");
    					}
    				}
    			}
    		}
    
    		// Register bean as disposable.
    		try {
    			registerDisposableBeanIfNecessary(beanName, bean, mbd);
    		}
    		catch (BeanDefinitionValidationException ex) {
    			throw new BeanCreationException(
    					mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
    		}
    
    		return exposedObject;
    	}
    	
    	// org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)
    	protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
    		if (System.getSecurityManager() != null) {
    			AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
    				invokeAwareMethods(beanName, bean);
    				return null;
    			}, getAccessControlContext());
    		}
    		else {
    			invokeAwareMethods(beanName, bean);
    		}
    
    		Object wrappedBean = bean;
    		if (mbd == null || !mbd.isSynthetic()) {
    			wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    		}
    
    		try {
    			invokeInitMethods(beanName, wrappedBean, mbd);
    		}
    		catch (Throwable ex) {
    			throw new BeanCreationException(
    					(mbd != null ? mbd.getResourceDescription() : null),
    					beanName, "Invocation of init method failed", ex);
    		}
    		if (mbd == null || !mbd.isSynthetic()) {
    			// step into ...
    			// 我们自定义用于自调用注入的beanPostProcessor在这里执行
    			// 题外话:aop代理类(beanPostProcessor)也是在这里生成代理实例
    			wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
    		}
    
    		return wrappedBean;
    	}
    	
    	// org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization
    	@Override
    	public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
    			throws BeansException {
    
    		// 初始值设为入参中的bean真实实例
    		Object result = existingBean;
    		for (BeanPostProcessor processor : getBeanPostProcessors()) {
    			// step into ...
    			// 可以想到,这里的result是前一次循环的值
    			// 通过debug可以观察到BeanPostProcessors的执行顺序:
    			// 		...
    			// 		AnnotationAwareAspectAutoProxyCreator(AOP切面用于生成代理实例的beanPostProcessor,并返回代理实例)
    			// 		InjectBeanSelfProcessor(我们自定义,用于注入代理对象的beanPostProcessor)
    			// 		CommonAnnotationBeanPostProcessor
    			// 		AutowiredAnnotationBeanPostProcessor
    			// 		...
    			Object current = processor.postProcessAfterInitialization(result, beanName);
    			if (current == null) {
    				return result;
    			}
    			result = current;
    		}
    		return result;
    	}
    
    	// AbstractAutoProxyCreator 与 AnnotationAwareAspectAutoProxyCreator 的关系见下图
    	// org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#postProcessAfterInitialization
    	@Override
    	public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
    		if (bean != null) {
    			Object cacheKey = getCacheKey(bean.getClass(), beanName);
    			// step into ...
    			// 如果earlyProxyReferences缓存不存在A的实例时,返回其代理实例
    			// 问题就出在这里,A在执行这段代码时,发现缓存中是有他自己的真实实例的,故直接返回了
    			if (this.earlyProxyReferences.remove(cacheKey) != bean) {
    				// 返回AOP代理实例,不是重点
    				return wrapIfNecessary(bean, beanName, cacheKey);
    			}
    		}
    		// 于是就通过这里,A就直接给自己注入bean(入参的bean指A的真实对象)
    		return bean;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183

    在这里插入图片描述

    附上我们自定义用于自调用注入代理实例的beanPostProcessor

    	@Component
    	public class InjectBeanSelfProcessor
    	        implements BeanPostProcessor
    	{
    	    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    	        return bean;
    	    }
    	    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    	    	//如果Bean实现了BeanSelfAware标识接口,就将代理对象注入
    	        if(bean instanceof BeanSelfAware) {
    	        	//即使是prototype Bean也可以使用此种方式
    	            ((BeanSelfAware) bean).setSelf(bean); 
    	        }
    	        return bean;
    	    }
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3. 依赖循环下该注入方式是如何裂开的

    前面的随笔对依赖循环的过程已经说的过于详细了,这里放一张简图直接略过:

    请添加图片描述

    现在我们回过看一下前面伏笔:
    addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
    该方法原意为B实例提供了返回A代理的出口,但有一个"副作用",让我们走进getEarlyBeanReference(beanName, mbd, bean)

    	// org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#getEarlyBeanReference
    	protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {
    		// 回顾可知,这里的初始值为bean的真实实例
    		Object exposedObject = bean;
    		if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
    			// 跟前面的一样,这里将访问AOP切面的BeanPostProcessor用于为B返回A实例
    			for (BeanPostProcessor bp : getBeanPostProcessors()) {
    				if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
    					SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
    					// step into ...
    					exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
    				}
    			}
    		}
    		return exposedObject;
    	}
    
    	// org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#getEarlyBeanReference
    	@Override
    	public Object getEarlyBeanReference(Object bean, String beanName) {
    		Object cacheKey = getCacheKey(bean.getClass(), beanName);
    		// 在这里缓存了A的真实实例
    		this.earlyProxyReferences.put(cacheKey, bean);
    		// 这里返回A的代理实例
    		return wrapIfNecessary(bean, beanName, cacheKey);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    线程和进程的优缺点
    【云原生 | Kubernetes 系列】----污点与容忍
    简单版的采用前后端分离模式实现SpingBoot新增&查询功能
    畜牧猪舍养殖成功 管理效率提高的背后原因
    数据分析Pandas专栏---第三章<Pandas合并list和字典>
    扑克牌顺子题
    一文带你快速了解【内部类】
    MYSQL 敏感数据加密后进行模糊查询
    哈佛架构 VS 冯·诺依曼架构
    [系统安全] 逆向工程进阶篇之对抗逆向分析
  • 原文地址:https://blog.csdn.net/weixin_43638238/article/details/127132338