Method 属于 Java 标准反射 API,位于 java.lang.reflect 包下。主要用于在运行时获取和操作类的方法,支持动态调用。
通过 Class 对象的 getMethod 等方法获取 Method 对象,用于获取方法的详细信息,如方法名称、返回类型、参数类型等。
示例:
- Class> clazz = MyClass.class;
- Method method = clazz.getMethod("exampleMethod", String.class, int.class);
MethodSignature 属于 Spring AOP 框架,位于 org.aspectj.lang.reflect 包下。主要用于 Spring AOP 中,用于在切面中获取被代理方法的签名信息,方便切面进行条件匹配、日志记录等操作。
通过 JoinPoint 对象获取 MethodSignature 对象。用于获取切点方法的详细信息,如方法名称、返回类型、参数类型等。
示例:
- @Aspect
- public class MyAspect {
-
- @Before("execution(* com.example.MyClass.exampleMethod(..))")
- public void beforeAdvice(JoinPoint joinPoint) {
- MethodSignature signature = (MethodSignature) joinPoint.getSignature();
- String methodName = signature.getName();
- Class> returnType = signature.getReturnType();
- // 其他方法信息...
- }
- }
框架不同
获取方式不同
用途不同