AOP引入的使用与源码实现

通知是对目标对象方法的增强,而引入可以动态为目标对象实现新的接口,实现对类的增强。

引入的使用

目标类

public class DogService {
	public void hi() {
		System.out.println("wangwang");
	}
}

增强接口

public interface CatService {
	void eat();
}

增强接口实现类

public class CatServiceImpl implements CatService {
	@Override
	public void eat() {
		System.out.println("cat eat");
	}
}

切面类

package com.morris.spring.config;

import com.morris.spring.service.CatService;
import com.morris.spring.service.CatServiceImpl;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
@Aspect
public class IntroduceConfig {

	@DeclareParents(value = "com.morris.spring.service.*+", defaultImpl = CatServiceImpl.class)
	private CatService catService;

}

测试类

package com.morris.spring.demo.annotation;

import com.morris.spring.config.IntroduceConfig;
import com.morris.spring.service.CatService;
import com.morris.spring.service.DogService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class IntroduceDemo {

	public static void main(String[] args) {

		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
		applicationContext.register(DogService.class);
		applicationContext.register(IntroduceConfig.class);
		applicationContext.refresh();
		DogService dogService = applicationContext.getBean(DogService.class);
		dogService.hi();
		CatService catService = (CatService) dogService;
		catService.eat();
	}
}

运行结果如下:

cat eat
wangwang

从运行结果可以发现DogService拥有了CatService接口的能力,可以使用arthas工具导出内存中动态代理生成的对象:

public class DogService$$EnhancerBySpringCGLIB$$15587e5c extends DogService implements CatService, SpringProxy, Advised, Factory {

源码实现

注解的扫描

解析带有@Aspect注解类下面的有@DeclareParents注解的属性:

org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory#getAdvisors

public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory aspectInstanceFactory) {
	... ...

	// Find introduction fields.
	// 引入
	for (Field field : aspectClass.getDeclaredFields()) {
		Advisor advisor = getDeclareParentsAdvisor(field);
		if (advisor != null) {
			advisors.add(advisor);
		}
	}

	return advisors;
}

遍历所有带有@DeclareParents注解的属性,封装成为DeclareParentsAdvisor对象。

org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory#getDeclareParentsAdvisor

private Advisor getDeclareParentsAdvisor(Field introductionField) {
	DeclareParents declareParents = introductionField.getAnnotation(DeclareParents.class);
	if (declareParents == null) {
		// Not an introduction field
		return null;
	}

	if (DeclareParents.class == declareParents.defaultImpl()) {
		throw new IllegalStateException("'defaultImpl' attribute must be set on DeclareParents");
	}

	return new DeclareParentsAdvisor(
			introductionField.getType(), declareParents.value(), declareParents.defaultImpl());
}

目标类的匹配

调用IntroductionAdvisor.getClassFilter().matches()进行类的匹配,上面注入的DeclareParentsAdvisor对象是IntroductionAdvisor的子类,也就是会根据@DeclareParents注解中指定的表达式与类的权限定名进行匹配。

org.springframework.aop.support.AopUtils#canApply(org.springframework.aop.Advisor, java.lang.Class<?>, boolean)

public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions) {
	if (advisor instanceof IntroductionAdvisor) {
		return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass);
	}
	else if (advisor instanceof PointcutAdvisor) {
		PointcutAdvisor pca = (PointcutAdvisor) advisor;
		return canApply(pca.getPointcut(), targetClass, hasIntroductions);
	}
	else {
		// It doesn't have a pointcut so we assume it applies.
		return true;
	}
}

与方法的增强一样,如果匹配到了目标类就会为目标类生成代理对象。

目标方法的调用

先上面的构造方法:

public DeclareParentsAdvisor(Class<?> interfaceType, String typePattern, Class<?> defaultImpl) {
	this(interfaceType, typePattern,
			new DelegatePerTargetObjectIntroductionInterceptor(defaultImpl, interfaceType));
}

将目标类封装到了DelegatePerTargetObjectIntroductionInterceptor,而DelegatePerTargetObjectIntroductionInterceptor是一个MethodInterceptor,最终目标方法的执行都会调到MethodInterceptor.invoke():

public Object invoke(MethodInvocation mi) throws Throwable {
	if (isMethodOnIntroducedInterface(mi)) {
		Object delegate = getIntroductionDelegateFor(mi.getThis());

		// Using the following method rather than direct reflection,
		// we get correct handling of InvocationTargetException
		// if the introduced method throws an exception.
		// 调用目标方法
		Object retVal = AopUtils.invokeJoinpointUsingReflection(delegate, mi.getMethod(), mi.getArguments());

		// Massage return value if possible: if the delegate returned itself,
		// we really want to return the proxy.
		if (retVal == delegate && mi instanceof ProxyMethodInvocation) {
			retVal = ((ProxyMethodInvocation) mi).getProxy();
		}
		return retVal;
	}

	return doProceed(mi);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

morris131

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值