Spring AOP 创建代理对象

 AnnotationAwareAspectJAutoProxyCreator【InstantiationAwareBeanPostProcessor】 的作用:

 1.创建对象之前调用 调用postProcessBeforeInstantiation

判断当前bean是否在advisedBeans中(保存了所有需要增强bean)

  2.isInfrastructureClass(beanClass)

判断当前bean是否是基础类型的Advice、Pointcut、Advisor、AopInfrastructureBean,
*        或者是否是切面(@Aspect)
@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		Object cacheKey = getCacheKey(beanClass, beanName);

		if (beanName == null || !this.targetSourcedBeans.contains(beanName)) {
			if (this.advisedBeans.containsKey(cacheKey)) {
				return null;
			}
			if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
				this.advisedBeans.put(cacheKey, Boolean.FALSE);
				return null;
			}
		}

 

@Override
	protected boolean isInfrastructureClass(Class<?> beanClass) {
		// Previously we setProxyTargetClass(true) in the constructor, but that has too
		// broad an impact. Instead we now override isInfrastructureClass to avoid proxying
		// aspects. I'm not entirely happy with that as there is no good reason not
		// to advise aspects, except that it causes advice invocation to go through a
		// proxy, and if the aspect implements e.g the Ordered interface it will be
		// proxied by that interface and fail at runtime as the advice method is not
		// defined on the interface. We could potentially relax the restriction about
		// not advising aspects in the future.
		return (super.isInfrastructureClass(beanClass) || this.aspectJAdvisorFactory.isAspect(beanClass));
	}
3.是否需要跳过
*        1)、获取候选的增强器(切面里面的通知方法)【List<Advisor> candidateAdvisors】
*           每一个封装的通知方法的增强器是 InstantiationModelAwarePointcutAdvisor;
*           判断每一个增强器是否是 AspectJPointcutAdvisor 类型的;返回true
*        2)、永远返回false

 

shouldSkip(beanClass, beanName)

 

 

 

 


2.执行创建对象的后置处理器

postProcessAfterInitialization

1.return wrapIfNecessary(bean, beanName, cacheKey);//包装如果需要的情况下

Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);

  1.1>获取当前bean的所有增强器

List<Advisor> candidateAdvisors = findCandidateAdvisors();

  1.2>获取当前bean可用的增强器

List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);

  1.3>给增强器排序

      eligibleAdvisors = sortAdvisors(eligibleAdvisors);

2.1 保存到advisedBeans中

this.advisedBeans.put(cacheKey, Boolean.TRUE);

2.2创建代理对象,spring自动决定选择jdk还是cplib代理 创建代理对象的时候 传入的增强器advised,

Object proxy = createProxy(
      bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));

3.给容器中返回当前组件使用cglib增强了的代理对象

 

 创建代理对象,Spring自动选择jdk动态代理还是cglib动态代理

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值