MyMethodInterceptor.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package example.aop.proxy;
  2. import org.aopalliance.intercept.MethodInterceptor;
  3. import org.aopalliance.intercept.MethodInvocation;
  4. import org.springframework.core.convert.support.DefaultConversionService;
  5. import org.springframework.data.jpa.repository.Query;
  6. import org.springframework.data.repository.core.support.RepositoryFactorySupport;
  7. public class MyMethodInterceptor implements MethodInterceptor {
  8. @Override
  9. public Object invoke(MethodInvocation invocation) throws Throwable {
  10. Query query = invocation.getMethod().getAnnotation(Query.class);
  11. if(query != null) {
  12. System.out.println("is native: " + query.nativeQuery());
  13. System.out.println("sql = " + query.value());
  14. }else{
  15. System.out.println("name = " + invocation.getMethod().getName());
  16. }
  17. return isBaseType(invocation.getMethod().getReturnType()) ? 0 : null;
  18. }
  19. /**
  20. * 判断object是否为基本类型
  21. * @return
  22. */
  23. public static boolean isBaseType(Class clazz) {
  24. String className = clazz.getTypeName();
  25. if (className.equals("int") ||
  26. className.equals("byte") ||
  27. className.equals("long") ||
  28. className.equals("double") ||
  29. className.equals("float") ||
  30. className.equals("char") ||
  31. className.equals("short") ||
  32. className.equals("boolean")) {
  33. return true;
  34. }
  35. return false;
  36. }
  37. }