public class UserDaoProxy{ private Object obj; public UserDaoProxy(Object obj) { super(); this.obj = obj; } //给目标对象,生成代理对象 public Object getProxyInstance(){ return Proxy.newProxyInstance( obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("开启事务。。。"); //执行目标对象方法 Object o=method.invoke(obj, args); System.out.println("提交事务。。。。"); return o; } }); } }
时间: 2024-10-09 01:40:46