Cglib实现AOP
// 切所有方法@Testpublic void cglib1() {UserService target = new UserService();// 通过cglib实现AOPEnhancer enhancer = new Enhancer();enhancer.setSuperclass(UserService.class);// 定义额外逻辑,也就是代理逻辑// o:代理对象; method:被代理类方法; objects:方法参数; methodProxy:代理类方法enhancer.setCallbacks(new Callback[]{(MethodInterceptor) (o, method, objects, methodProxy) -> {System.out.println("before...");Object result = methodProxy.invoke(target, objects);System.out.println("after...");return result;}});// 动态代理所创建出来的UserService代理对象UserService userService = (UserService) enhancer.create();// 执行这个userService的test方法时,就会额外会执行一些其他逻辑userService.test();}// 切指定方法@Testpublic void cglib2() {UserService target = new UserService();// 通过cglib实现AOPEnhancer enhancer = new Enhancer();enhancer.setSuperclass(UserService.class);// 定义多个代理逻辑, 数组类型enhancer.setCallbacks(new Callback[]{(MethodInterceptor) (o, method, objects, methodProxy) -> {System.out.println("before...");Object result = methodProxy.invoke(target, objects);System.out.println("after...");return result;}, NoOp.INSTANCE});// 设置切面匹配逻辑, return对应执行的callback数组enhancer.setCallbackFilter(method -> {if("test".equals(method.getName())) {return 0;}return 1;});// 动态代理所创建出来的UserService代理对象UserService userService = (UserService) enhancer.create();// 执行这个userService的test方法时,就会额外会执行一些其他逻辑userService.test();userService.print();}
下面执行结果, 可看出, 只有test执行了额外逻辑, print执行了空代理逻辑
before...
test---------
after...
print---------
JDK动态代理实现AOP
@Testpublic void jdkProxy1() {UserService target = new UserService();Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{UserInterface.class}, (proxy1, method, args) -> {System.out.println("before...");Object result = method.invoke(target, args);System.out.println("after...");return result;});UserInterface userInterface = (UserInterface) proxy;userInterface.test();}
ProxyFactory实现AOP
spring对cglib和jdk动态代理进行了封装, 叫做ProxyFactory代理工厂, 会自动根据是否是接口, 去切换.如果实现了接口, 用jdk动态代理, 否则用cglib动态代理.
// 代理类 - cglib动态代理@Testpublic void proxyFactory1() {UserService target = new UserService();ProxyFactory proxyFactory = new ProxyFactory();proxyFactory.setTarget(target);proxyFactory.addAdvice(new MethodInterceptor() {@Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {System.out.println("before...");Object proceed = invocation.proceed();System.out.println("after...");return proceed;}});UserService userService = (UserService) proxyFactory.getProxy();userService.test();}// 代理接口 - jdk动态代理@Testpublic void proxyFactory2() {UserService target = new UserService();ProxyFactory proxyFactory = new ProxyFactory();proxyFactory.setTarget(target);proxyFactory.addAdvice(new MethodInterceptor() {@Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {System.out.println("before...");Object proceed = invocation.proceed();System.out.println("after...");return proceed;}});UserInterface userService = (UserInterface) proxyFactory.getProxy();userService.test();}
Spring实现AOP
AOP概念
Aspect
Join point
Adivce
PointCut
Weaving