aop bean增强

主要的两个类
org.springframework.aop.framework.AbstractAdvisingBeanPostProcessor
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator

AOP下的实现类
org.springframework.validation.beanvalidation.MethodValidationPostProcessor

org.springframework.beans.factory.xml.NamespaceHandler 定制化xsd扩展的时候的解析器

org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator

org.springframework.aop.framework.AbstractAdvisingBeanPostProcessor#postProcessAfterInitialization

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
1、 if (this.advisor == null || bean instanceof AopInfrastructureBean) {
// Ignore AOP infrastructure such as scoped proxies.
return bean;
}

2、 if (bean instanceof Advised) {
Advised advised = (Advised) bean;
if (!advised.isFrozen() && isEligible(AopUtils.getTargetClass(bean))) { 判断能不能处理 增强
// Add our local Advisor to the existing proxy’s Advisor chain…
if (this.beforeExistingAdvisors) {

				advised.addAdvisor(0, this.advisor);
			}
			else {
				advised.addAdvisor(this.advisor);      MethodValidationPostProcessor  方法级增强 将创建好的默认对象加入到代理类中
			}
			return bean;
		}
	}

3、 if (isEligible(bean, beanName)) { 是不是有代理资格 beanName字段没啥用
ProxyFactory proxyFactory = prepareProxyFactory(bean, beanName);
if (!proxyFactory.isProxyTargetClass()) {
evaluateProxyInterfaces(bean.getClass(), proxyFactory);
}
proxyFactory.addAdvisor(this.advisor);
customizeProxyFactory(proxyFactory);
return proxyFactory.getProxy(getProxyClassLoader());
}

	// No proxy needed.

4、 return bean;
}

1、 部分case不做增强
aop*接口 标记不会被代理 默认不做任何处理
org.springframework.aop.framework.AopInfrastructureBean
advisor 具体增强实现类没有 不做任何处理
org.springframework.aop.Advisor 赋值 不同的子类 不同的赋值方式
org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor#setBeanFactory
org.springframework.validation.beanvalidation.MethodValidationPostProcessor#afterPropertiesSet
org.springframework.aop.support.DefaultPointcutAdvisor 创建默认的对象

2、 没有冻结并且目标类可以代理则将 具体切点植入到对象中

aop操作接口 (据说任何AOP的类均可转换成这个接口)
由包含AOP代理工厂配置的类实现的接口。此配置包括拦截器和其他建议,顾问以及代理接口。
org.springframework.aop.framework.Advised

org.springframework.aop.framework.Advised#isFrozen 被冻结对象无法修改

org.springframework.aop.support.AopUtils#getTargetClass 返回AOP代理的目标类 如果非AOP代理则返回该对象

beforeExistingAdvisors 根据这个值决定植入的次序是 滞后还是提前

3、 当前类能否代理
初始化一个ProxyFactory 初始化默认 proxyTargetClass =false (从当前拷贝 如果处理过则 proxyTargetClass 可能为true)
org.springframework.aop.framework.ProxyProcessorSupport#evaluateProxyInterfaces
增加AOP增加实例
org.springframework.aop.framework.AbstractAdvisingBeanPostProcessor#customizeProxyFactory 定制化需求 无实现
返回代理类

4、 不符合任何信息 不做代理

org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization
容器初始化 触发对象实例化 触发创建代理

org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#postProcessBeforeInstantiation

@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
	Object cacheKey = getCacheKey(beanClass, beanName);

	if (!StringUtils.hasLength(beanName) || !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;
		}
	}

	// Create proxy here if we have a custom TargetSource.
	// Suppresses unnecessary default instantiation of the target bean:
	// The TargetSource will handle target instances in a custom fashion.
	TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
	if (targetSource != null) {
		if (StringUtils.hasLength(beanName)) {
			this.targetSourcedBeans.add(beanName);
		}
		Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
		Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
		this.proxyTypes.put(cacheKey, proxy.getClass());
		return proxy;
	}

	return null;
}

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);
if (this.earlyProxyReferences.remove(cacheKey) != bean) {
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}

beanPostProcessors = {CopyOnWriteArrayList@4802} size = 9
4 = {AnnotationAwareAspectJAutoProxyCreator@5782} “proxyTargetClass=true; optimize=false; opaque=false; exposeProxy=false; frozen=false”
5 = {MethodValidationPostProcessor@5758} “proxyTargetClass=true; optimize=false; opaque=false; exposeProxy=false; frozen=false”

上一篇:AOP原理-AnnotationAwareAspectJAutoProxyCreator执行时机 || InstantiationAwareBeanPostProcessor


下一篇:spring-ioc图解