Annotation & Reflection 学习 及 问题

最近在学习Anntation和Reflection,一直希望能做一个实际的应用.

所以,假象了一个例子,比如说在方法前加上Annotation来表明方法是否要进行事务处理,然后通过动态代理来执行.

1 MyAnnotationI

java 代码
  1. public interface MyAnnotationI {   
  2.     public void printString();   
  3.   
  4.     public void printString1();   
  5. }   

java 代码
  1. public class MyAnnotationImpl implements MyAnnotationI {   
  2.     @MyAnnotation(isTransaction ="yes")   
  3.     public void printString() {   
  4.         System.out.print("\n"+" don some thing");   
  5.     }   
  6.        
  7.     @MyAnnotation(isTransaction="yes")   
  8.     public void printString1() {   
  9.         System.out.print("\n"+" good work");   
  10.     }   
  11.        
  12. }  

 

3 然后是定义的Annotation

java 代码
  1. @Retention(java.lang.annotation.RetentionPolicy.RUNTIME)   
  2. public @interface MyAnnotation {   
  3.     String isTransaction();   
  4. }   

 

我希望能在执行 MyAnnotationImpl 的;两个方法是做一个判断,来看是否执行事务代码. 下面是我写的代理类

 

java 代码
  1. public class MyHandler implements InvocationHandler {   
  2.     private Object o;   
  3.   
  4.     public MyHandler(Object delegate) {   
  5.         o = delegate;   
  6.     }   
  7.   
  8.     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {   
  9.         Object a = null;   
  10.         try{   
  11.         System.out.print("\n"+"Start transaction ");   
  12.         a = method.invoke(o, args);   
  13.         System.out.print("\n"+"end transaction ");   
  14.         }   
  15.         catch(Exception e)   
  16.         {   
  17.             System.out.print("\n"+"Exception");   
  18.         }   
  19.         return null;   
  20.     }   
  21.   
  22. }  

 

也就是希望在方法上有isTransaction ="yes"的方法前后执行两行代码.

下面是运行代码

 

java 代码
  1. public class MyAnnotationTest {   
  2.   
  3.     
  4.   
  5.     public  void dynamicProxy()   
  6.     {   
  7.            
  8.     }   
  9.     public static void main(String[] args) {   
  10.         try {   
  11.             Class bean = Class.forName("com.my.annotation.one.MyAnnotationImpl");   
  12.             Method method[] = bean.getMethods();   
  13.             for (int i = 0; i < method.length; i++) {   
  14.                 if (method[i].getName().equals("printString1")) {   
  15.                     MyAnnotation an = method[i].getAnnotation(MyAnnotation.class);   
  16.                     if(an.isTransaction().equals("yes"))   
  17.                     {   
  18.                       Object o =   bean.newInstance();   
  19.                         InvocationHandler handler = new MyHandler(bean.newInstance());   
  20.                         MyAnnotationI proxy = (MyAnnotationI) Proxy.newProxyInstance(o.getClass().getClassLoader(), o.getClass().getInterfaces(),   
  21.                                 handler);   
  22.                         Method m =     proxy.getClass().getDeclaredMethod("printString1"null);   
  23.   
  24.                         m.invoke(proxy, new Object[]{});   
  25.                     }   
  26.                 }   
  27.   
  28.             }   
  29.   
  30.         } catch (Exception e) {   
  31.             System.out.println(e.toString());   
  32.         }    
  33.   
  34.     }   
  35.   
  36. }  

 

输出结果如下:

Start transaction
 good work
end transaction

 这样看似通过Annotation和Reflection 实现了一个事物,但是我有些问题.

1 这里是我指明了要调用哪个方法的,如果我也不知道具体执行那个方法.需要在运行时动态的去判定,我该怎么做?

2 我在 proxy.getClass().getDeclaredMethod("printString1"null)中调用的无参数的方法,如果我的执行方法有参数, 程序怎么知道有那些 参数,如果得到要调用的方法?

希望明白的朋友给我指点一二.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值