EatMethodMatcher.java 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package com.sf.aop.advisor;
  2. import org.springframework.aop.MethodMatcher;
  3. import java.lang.reflect.Method;
  4. // 在方法级别的拦截
  5. // 使用接口 MethodMatcher
  6. // 在实现方法中 判断是否要拦截
  7. public class EatMethodMatcher implements MethodMatcher {
  8. /**
  9. * 监控接口比如BaseService,没有重载方法,每一个方法名称都是唯一的,
  10. * 此时采用 static 检测方式,只根据方法名称来进行判断,如果有重载方法,
  11. * 可以使用下方的 matches() 方法
  12. * 业务:只想对 Person 类中的 eat() 方法提供织入
  13. *
  14. * @param method 接口中的某一个方法
  15. * @param targetClass 接口中的实现类
  16. * @return
  17. */
  18. @Override
  19. public boolean matches(Method method, Class<?> targetClass) {
  20. String methodName = method.getName();
  21. if ("eat".equals(methodName)) {
  22. return true;
  23. }
  24. return false;
  25. }
  26. @Override
  27. public boolean isRuntime() {
  28. return false;
  29. }
  30. @Override
  31. public boolean matches(Method method, Class<?> targetClass, Object... args) {
  32. return false;
  33. }
  34. }