一、AOP是什麼 AOP(面向切麵編程),可以說是一種編程思想,其中的Spring AOP和AspectJ都是現實了這種編程思想。相對OOP(面向過程編程)來說,提供了另外一種編程方式,對於OOP過程中產生的橫切性問題,這些橫切性與業務無關,可以通過預編譯方式和運行期動態代理來實現。比如可以應用在: ...
一、AOP是什麼
AOP(面向切麵編程),可以說是一種編程思想,其中的Spring AOP和AspectJ都是現實了這種編程思想。相對OOP(面向過程編程)來說,提供了另外一種編程方式,對於OOP過程中產生的橫切性問題,這些橫切性與業務無關,可以通過預編譯方式和運行期動態代理來實現。比如可以應用在:日誌記錄、性能監控、事務管理等。
二、AOP的基本概念
Aspect(切麵):通常來說是一個類,裡面定義了切點和通知,Spring AOP中可以用@AspectJ來標註這個類是切麵;
Join point(連接點):可以理解成目標對象中的方法,該方法是要被增強的方法,也就是我們要作用的一個切入點;
Pointcut(切點):切點可以理解成連接點的集合;
Target object(目標對象):被代理的對象,也就是目標對象;
AOP proxy(代理對象):把被代理的對象織入了增強後的對象;
Weaving(織入):把增強也就是代理邏輯加入到目標對象上的過程;
Advice(通知):用於指定在特定連接點上的增強的位置;
① Before advice(前置通知):在目標方法被調用之前調用通知;
② After returning advice(返回通知):在目標方法成功執行之後調用通知;
③ After throwing advice(異常通知):在目標方法拋出異常後調用通知;
④ After (finally) advice(後置通知):在目標方法完成之後調用通知(不論是否出現異常都會執行,finally中調用);
⑤ Around advice(環繞通知):圍繞連接點(目標方法)的通知。可以在方法調用前後執行自定義行為;
三、代理的實現方式
我們知道AOP可以通過預編譯的方式和運行期動態代理來實現,那麼代理的實現方式有哪些呢?
我們定義一個介面類:UserService
package com.toby.service; /** * @desc: user 業務介面類 * @author: toby * @date: 2019/8/4 23:28 */ public interface UserService { /** * 添加 */ void add(); /** * say hello * @param name * @return */ String say(String name); }
在定義一個實現類UserServiceImpl
package com.toby.service.impl; import com.toby.service.UserService; import org.springframework.stereotype.Service; /** * @desc: user業務的實現類 * @author: toby * @date: 2019/8/4 23:29 */ @Service public class UserServiceImpl implements UserService { @Override public void add() { System.out.println("執行UserServiceImpl的add方法"); } @Override public String say(String name) { System.out.println("執行UserServiceImpl的say方法 args = " + name); return "hello " + name; } }
第一種:靜態代理
定義一個靜態代理類,需要實現UserService介面:
package com.toby.proxy; import com.toby.service.UserService; /** * @desc: 靜態代理 * @author: toby * @date: 2019/8/4 23:30 */ public class StaticProxy implements UserService { private UserService userService; public StaticProxy(UserService userService){ this.userService = userService; } @Override public void add() { System.out.println("添加日誌開始"); userService.add(); System.out.println("添加日誌結束"); } @Override public String say(String name) { return ""; } }
第二種:Jdk動態代理
package com.toby.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * @desc: JDK動態代理 實現一個介面:InvocationHandler * JDK的動態代理機制只能代理實現了介面的類,而沒有實現介面的類就不能實現JDK的動態代理 * @author: toby * @date: 2019/8/4 23:34 */ public class JdkDynamicProxy implements InvocationHandler { /** * 目標對象 */ private Object targetObject; public Object createJdkProxy(final Object targetObject){ this.targetObject = targetObject; return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(), this.targetObject.getClass().getInterfaces(),this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //寫對應的增強代碼 System.out.println("Jdk日誌記錄開始"); //調用真正的業務方法 Object obj = method.invoke(this.targetObject,args); System.out.println("Jdk日誌記錄結束"); return obj; } }
第三種:Cglib動態代理
package com.toby.proxy; import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; import java.lang.reflect.Method; /** * @desc: CGLIB動態代理 cglib是針對類來實現代理的,他的原理是對指定的目標類生成一個子類, * 並覆蓋其中方法實現增強,但因為採用的是繼承,所以不能對final修飾的類進行代理 * @author: toby * @date: 2019/8/4 23:43 */ public class CglibDynamicProxy implements MethodInterceptor { /** * 目標對象 */ private Object targetObject; /** * 創建Cglib動態代理 * @param targetObject * @return */ public Object createCglibDynamicProxy(final Object targetObject){ this.targetObject = targetObject; //Cglib中的核心對象,該類用於生成代理對象 Enhancer enhancer = new Enhancer(); //指定委托類也就是目標對象為父類 enhancer.setSuperclass(this.targetObject.getClass()); //使用代理,需要一個對應的代理對象 enhancer.setCallback(this); return enhancer.create(); } @Override public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { System.out.println("CGLIB日誌記錄開始"); //委托類變成了父類。調用真正的服務提供者 Object obj = methodProxy.invoke(this.targetObject,args); System.out.println("CGLIB日誌記錄結束"); return obj; } }
JDK代理的實現方式是基於介面實現,代理類繼承Proxy,實現介面。而CGLIB繼承被代理的類來實現;這就是為什麼JDK動態代理需要實現介面的原因?Java是單繼承
下麵定義一個位元組碼生成器ByteCodeGenerator來一看究竟:
package com.toby.proxy.generator; import com.toby.service.impl.UserServiceImpl; import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.MethodInterceptor; import sun.misc.ProxyGenerator; import java.io.File; import java.io.FileOutputStream; import java.lang.reflect.Modifier; import java.nio.file.Files; /** * @desc: 位元組碼生成器 * @author: toby * @date: 2019/8/5 0:05 */ public class ByteCodeGenerator { /** * 根據目標對象生成位元組碼(Jdk) * @param target * @param <T> * @return */ public static <T> byte[] generatorByteCodeByJdkProxy(T target){ int accessFlags = Modifier.PUBLIC | Modifier.FINAL; byte [] codes = ProxyGenerator.generateProxyClass("Proxy$"+target.getClass().getName(), target.getClass().getInterfaces(),accessFlags); return codes; } /** * 根據目標對象生成位元組碼(Cglib) * @param target * @param <T> * @return * @throws Exception */ public static <T> byte[] generatorByteCodeByCglib(final T target) throws Exception { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(target.getClass()); enhancer.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> methodProxy.invoke(target,objects)); enhancer.create(); byte [] codes = enhancer.getStrategy().generate(enhancer); return codes; } public static void main(String[] args) { /** * 測試jdk */ try { byte [] codes = ByteCodeGenerator.generatorByteCodeByJdkProxy(new UserServiceImpl()); File file = new File(System.getProperty("user.dir")+"/spring-aop/target/Proxy$UserServiceImpl.class"); Files.write(file.toPath(),codes); } catch (Exception e) { e.printStackTrace(); } /** * 測試cglib */ try { FileOutputStream out = new FileOutputStream(System.getProperty("user.dir")+"/spring-aop/target/Cglib$UserServiceImpl.class"); out.write(ByteCodeGenerator.generatorByteCodeByCglib(new UserServiceImpl())); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } }
Jdk生成的動態代理位元組碼:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package Proxy$com.toby.service.impl; import com.toby.service.UserService; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; public final class UserServiceImpl extends Proxy implements UserService { private static Method m1; private static Method m2; private static Method m3; private static Method m0; public UserServiceImpl(InvocationHandler var1) throws { super(var1); } public final boolean equals(Object var1) throws { try { return (Boolean)super.h.invoke(this, m1, new Object[]{var1}); } catch (RuntimeException | Error var3) { throw var3; } catch (Throwable var4) { throw new UndeclaredThrowableException(var4); } } public final String toString() throws { try { return (String)super.h.invoke(this, m2, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } public final void add() throws { try { super.h.invoke(this, m3, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } public final int hashCode() throws { try { return (Integer)super.h.invoke(this, m0, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } static { try { m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object")); m2 = Class.forName("java.lang.Object").getMethod("toString"); m3 = Class.forName("com.toby.service.UserService").getMethod("add"); m0 = Class.forName("java.lang.Object").getMethod("hashCode"); } catch (NoSuchMethodException var2) { throw new NoSuchMethodError(var2.getMessage()); } catch (ClassNotFoundException var3) { throw new NoClassDefFoundError(var3.getMessage()); } } }
Cglib生成動態代理的位元組碼:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.toby.service.impl; import java.lang.reflect.Method; import org.springframework.cglib.core.ReflectUtils; import org.springframework.cglib.core.Signature; import org.springframework.cglib.proxy.Callback; import org.springframework.cglib.proxy.Factory; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; public class UserServiceImpl$$EnhancerByCGLIB$$b26297df extends UserServiceImpl implements Factory { private boolean CGLIB$BOUND; public static Object CGLIB$FACTORY_DATA; private static final ThreadLocal CGLIB$THREAD_CALLBACKS; private static final Callback[] CGLIB$STATIC_CALLBACKS; private MethodInterceptor CGLIB$CALLBACK_0; private static Object CGLIB$CALLBACK_FILTER; private static final Method CGLIB$add$0$Method; private static final MethodProxy CGLIB$add$0$Proxy; private static final Object[] CGLIB$emptyArgs; private static final Method CGLIB$equals$1$Method; private static final MethodProxy CGLIB$equals$1$Proxy; private static final Method CGLIB$toString$2$Method; private static final MethodProxy CGLIB$toString$2$Proxy; private static final Method CGLIB$hashCode$3$Method; private static final MethodProxy CGLIB$hashCode$3$Proxy; private static final Method CGLIB$clone$4$Method; private static final MethodProxy CGLIB$clone$4$Proxy; static void CGLIB$STATICHOOK2() { CGLIB$THREAD_CALLBACKS = new ThreadLocal(); CGLIB$emptyArgs = new Object[0]; Class var0 = Class.forName("com.toby.service.impl.UserServiceImpl$$EnhancerByCGLIB$$b26297df"); Class var1; Method[] var10000 = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods()); CGLIB$equals$1$Method = var10000[0]; CGLIB$equals$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$1"); CGLIB$toString$2$Method = var10000[1]; CGLIB$toString$2$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$2"); CGLIB$hashCode$3$Method = var10000[2]; CGLIB$hashCode$3$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$3"); CGLIB$clone$4$Method = var10000[3]; CGLIB$clone$4$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$4"); CGLIB$add$0$Method = ReflectUtils.findMethods(new String[]{"add", "()V"}, (var1 = Class.forName("com.toby.service.impl.UserServiceImpl")).getDeclaredMethods())[0]; CGLIB$add$0$Proxy = MethodProxy.create(var1, var0, "()V", "add", "CGLIB$add$0"); } final void CGLIB$add$0() { super.add(); } public final void add() { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } if (var10000 != null) { var10000.intercept(this, CGLIB$add$0$Method, CGLIB$emptyArgs, CGLIB$add$0$Proxy); } else { super.add(); } } final boolean CGLIB$equals$1(Object var1) { return super.equals(var1); } public final boolean equals(Object var1) { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } if (var10000 != null) { Object var2 = var10000.intercept(this, CGLIB$equals$1$Method, new Object[]{var1}, CGLIB$equals$1$Proxy); return var2 == null ? false : (Boolean)var2; } else { return super.equals(var1); } } final String CGLIB$toString$2() { return super.toString(); } public final String toString() { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy) : super.toString(); } final int CGLIB$hashCode$3() { return super.hashCode(); } public final int hashCode() { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } if (var10000 != null) { Object var1 = var10000.intercept(this, CGLIB$hashCode$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy); return var1 == null ? 0 : ((Number)var1).intValue(); } else { return super.hashCode(); } } final Object CGLIB$clone$4() throws CloneNotSupportedException { return super.clone(); } protected final Object clone() throws CloneNotSupportedException { MethodInterceptor var10000 = this.CGLIB$CALLBACK_0; if (var10000 == null) { CGLIB$BIND_CALLBACKS(this); var10000 = this.CGLIB$CALLBACK_0; } return var10000 != null ? var10000.intercept(this, CGLIB$clone$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy) : super.clone(); } public static MethodProxy CGLIB$findMethodProxy(Signature var0) { String var10000 = var0.toString(); switch(var10000.hashCode()) { case -1422568652: if (var10000.equals("add()V")) { return CGLIB$add$0$Proxy; } break; case -508378822: if (var10000.equals("clone()Ljava/lang/Object;")) { return CGLIB$clone$4$Proxy; } break; case 1826985398: if (var10000.equals("equals(Ljava/lang/Object;)Z")) { return CGLIB$equals$1$Proxy; } break; case 1913648695: if (var10000.equals("toString()Ljava/lang/String;")) { return CGLIB$toString$2$Proxy; } break; case 1984935277: if (var10000.equals("hashCode()I")) { return CGLIB$hashCode$3$Proxy; } } return null; } public UserServiceImpl$$EnhancerByCGLIB$$b26297df() { CGLIB$BIND_CALLBACKS(this); } public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) { CGLIB$THREAD_CALLBACKS.set(var0); } public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) { CGLIB$STATIC_CALLBACKS = var0; } private static final void CGLIB$BIND_CALLBACKS(Object var0) { UserServiceImpl$$EnhancerByCGLIB$$b26297df var1 = (UserServiceImpl$$EnhancerByCGLIB$$b26297df)var0; if (!var1.CGLIB$BOUND) { var1.CGLIB$BOUND = true; Object var10000 = CGLIB$THREAD_CALLBACKS.get(); if (var10000 == null) { var10000 = CGLIB$STATIC_CALLBACKS; if (var10000 == null) { return; } } var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0]; } } public Object newInstance(Callback[] var1) { CGLIB$SET_THREAD_CALLBACKS(var1); UserServiceImpl$$EnhancerByCGLIB$$b26297df var10000 = new UserServiceImpl$$EnhancerByCGLIB$$b26297df(); CGLIB$SET_THREAD_CALLBACKS((Callback[])null); return var10000; } public Object newInstance(Callback var1) { CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1}); UserServiceImpl$$EnhancerByCGLIB$$b26297df var10000 = new UserServiceImpl$$EnhancerByCGLIB$$b26297df(); CGLIB$SET_THREAD_CALLBACKS((Callback[])null); return var10000; } public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) { CGLIB$SET_THREAD_CALLBACKS(var3); UserServiceImpl$$EnhancerByCGLIB$$b26297df var10000 = new UserServiceImpl$$EnhancerByCGLIB$$b26297df; switch(var1.length) { case 0: var10000.<init>(); CGLIB$SET_THREAD_CALLBACKS((Callback[])null); return var10000; default: throw new IllegalArgumentException("Constructor not found"); } } public Callback getCallback(int var1) { CGLIB$BIND_CALLBACKS(this); MethodInterceptor var10000; switch(var1) { case 0: var10000 = this.CGLIB$CALLBACK_0; break; default: var10000 = null; } return var10000; } public void setCallback(int var1, Callback var2) { switch(var1) { case 0: this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2; default: } } public Callback[] getCallbacks() { CGLIB$BIND_CALLBACKS(this); return new Callback[]{this.CGLIB$CALLBACK_0}; } public void setCallbacks(Callback[] var1) { this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0]; } static { CGLIB$STATICHOOK2(); } }
第四種:Javassist動態代理(在動態位元組碼插樁詳解)
① 定義一個JavassistDynamicProxy實現Javassist動態代理:
package com.toby.proxy; import javassist.util.proxy.ProxyFactory; import javassist.util.proxy.ProxyObject; /** * @desc: javassist * @author: toby * @date: 2019/8/15 20:22 */ public class JavassistDynamicProxy { /** * 創建Javassist動態代理 * @param targetObject * @throws Exception * @return */ public Object createJavassistDynamicProxy(final Object targetObject)throws Exception { ProxyFactory factory = new ProxyFactory(); factory.setInterfaces(targetObject.getClass().getInterfaces()); Class<?> proxyClass = factory.createClass(); Object javassistProxy = proxyClass.newInstance(); ((ProxyObject)javassistProxy).setHandler((self,thisMethod,proceed,args)-> { //寫對應的增強代碼 System.out.println("Javassist日誌記錄開始"); //調用真正的業務方法 Object obj = thisMethod.invoke(targetObject,args); System.out.println("Javassist日誌記錄結束"); return obj; }); return javassistProxy; } }
② 定義一個JavassistBytecodeDynamicProxy實現Javassist動態代理:
package com.toby.proxy; import javassist.*; import java.lang.reflect.Field; /** * @desc: javassist 位元組碼動態代理 * @author: toby * @date: 2019/8/15 20:42 */ public class JavassistBytecodeDynamicProxy { /** * 創建Javassist位元組碼動態代理 * @param targetObject * @return * @throws Exception */ public static Object createJavassistBytecodeDynamicProxy(final Object targetObject) throws Exception { ClassPool pool = ClassPool.getDefault(); CtClass proxyClass = pool.makeClass("JavassistProxy" + "&" +targetObject.getClass().getName()); proxyClass.addInterface(pool.get(targetObject.getClass().getInterfaces()[0].getName())); proxyClass.addConstructor(CtNewConstructor.defaultConstructor(proxyClass)); proxyClass.addField(CtField.make("private " + targetObject.getClass().getName() + " targetObject;", proxyClass)); proxyClass.addMethod(CtNewMethod.make("public void add() { \n" + "System.out.println(\"Javassist位元組碼日誌記錄開始\");\n" + "targetObject.add();\n" + "System.out.println(\"Javassist位元組碼日誌記錄結束\");\n"+ "}", proxyClass)); Class<?> clazz = proxyClass.toClass(); Object bytecodeProxy = clazz.newInstance(); Field field = bytecodeProxy.getClass().getDeclaredField("targetObject"); field.setAccessible(true); field.set(bytecodeProxy,targetObject); return bytecodeProxy; } /** * 創建Javassist位元組碼動態代理2 * @param targetObject * @return * @throws Exception */ public static Object createJavassistBytecodeDynamicProxy2(final Object targetObject) throws Exception { ClassPool pool = ClassPool.getDefault(); pool.appendSystemPath(); CtClass ctl = pool.get(targetObject.getClass().getName()); ctl.setName("JavassistProxy" + "&" + targetObject.getClass().getName()); CtMethod ctMethod = ctl.getDeclaredMethod("add"); ctMethod.insertBefore("System.out.println(\"Javassist位元組碼2日誌記錄開始\");"); ctMethod.insertAfter("System.out.println(\"Javassist位元組碼2日誌記錄結束\");"); Class<?> clazz = ctl.toClass(); Object bytecodeProxy = clazz.newInstance(); return bytecodeProxy; } }
四、Spring AOP
Spring AOP提供兩種編程風格,詳細用法見Spring官網:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#aop-aspectj-support
① @AspectJ support(利用aspectj的註解)
② Schema-based AOP support(基於xml aop:config命名空間)
啟用@AspectJ支持
① 使用Java Configuration啟用@AspectJ支持:要使用Java @Configuration啟用@AspectJ支持,要添加@EnableAspectJAutoProxy註釋:
@Configuration @EnableAspectJAutoProxy public class AppConfig { }
② 使用XML配置啟用@AspectJ支持:要使用基於xml的配置啟用@AspectJ支持,可以使用aop:aspectj-autoproxy元素
<aop:aspectj-autoproxy/>
聲明一個Aspect
@Aspect @Component public class UserAspect { }
聲明一個Pointcut
@Pointcut("execution(* com.toby.service.UserService.*(..))")//the pointcut expression
public void pointCutExecution(){}//the pointcut signature
Spring AOP支持的9種切入點表達式
① execution:execution用於匹配方法執行 join points連接點,最小粒度方法
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?) 這裡問號表示當前項可以有也可以沒有,其中各項的語義如下 modifiers-pattern:方法的可見性,如public,protected; ret-type-pattern:方法的返回值類型,如int,void等; declaring-type-pattern:方法所在類的全路徑名,如com.spring.Aspect; name-pattern:方法名類型,如buisinessService(); param-pattern:方法的參數類型,如java.lang.String; throws-pattern:方法拋出的異常類型,如java.lang.Exception; example: @Pointcut("execution(* com.toby.dao.*.*(..))")//匹配com.toby.dao包下的任意介面和類的任意方法 @Pointcut("execution(public * com.toby.dao.*.*(..))")//匹配com.toby.dao包下的任意介面和類的public方法 @Pointcut("execution(public * com.toby.dao.*.*())")//匹配com.toby.dao包下的任意介面和類的public 無方法參數的方法 @Pointcut("execution(* com.toby.dao.*.*(java.lang.String, ..))")//匹配com.toby.dao包下的任意介面和類的第一個參數為String類型的方法 @Pointcut("execution(* com.toby.dao.*.*(java.lang.String))")//匹配com.toby.dao包下的任意介面和類的只有一個參數,且參數為String類型的方法 @Pointcut("execution(* com.toby.dao.*.*(java.lang.String))")//匹配com.toby.dao包下的任意介面和類的只有一個參數,且參數為String類型的方法 @Pointcut("execution(public * *(..))")//匹配任意的public方法 @Pointcut("execution(* te*(..))")//匹配任意的以te開頭的方法 @Pointcut("execution(* com.toby.dao.IndexDao.*(..))")//匹配com.toby.dao.IndexDao介面中任意的方法 @Pointcut("execution(* com.toby.dao..*.*(..))")//匹配com.toby.dao包及其子包中任意的方法 關於這個表達式的詳細寫法,可以參考官網:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#aop-pointcuts-examples
定義一