spring动态代理封装MethodInvocation。导致获取不到方法annotation原因.

因为spring动态代理封装MethodInvocation。导致获取不到方法annotation原因.

 

spring如果需要前后通知的话。。一般会实现MethodInterceptor

 public Object invoke(MethodInvocation invocation) throws Throwable

 

invocation.getMethod().getAnnotations(); // 根本得不到原来类方法上的annotation

因为JDK在动态代理的时候。生成的代理类。。。方法签名跟类中的签名是一样的。但不是原来的方法

 

 

 

sun.misc.ProxyGenerator.ProxyGenerator.generateProxyClass生成的字节写到文件里去

 

 Class[] interfaces=new Class[]{ITest.class};
  byte[] proxyClassFile = ProxyGenerator.generateProxyClass(proxyName, interfaces);
  File file = new File("class/"+proxyName+".class");
   FileOutputStream fos = new FileOutputStream(file);
   fos.write(proxyClassFile);
   fos.flush();
   fos.close();

 

 

CGLIB生成代理类CLASS文件:

  Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(InfoManager.class);
        enhancer.setCallback(auth);
        InfoManager m = (InfoManager) enhancer.create();
        try {
            ClassWriter cw = new ClassWriter(true);
            enhancer.generateClass(cw);
            byte[] buffer = cw.toByteArray();
            FileOutputStream out = new FileOutputStream("c:/tttttt");
            out.write(buffer);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

 

 

你可能感兴趣的:(spring动态代理封装MethodInvocation。导致获取不到方法annotation原因.)