/** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public interface Dog { //info方法声明 public void info(); //run方法声明 public void run(); }
/** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class GunDog implements Dog { //info方法实现,仅仅打印一个字符串 public void info() { System.out.println("我是一只猎狗"); } //run方法实现,仅仅打印一个字符串 public void run() { System.out.println("我奔跑迅速"); } }
import java.lang.reflect.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class MyInvokationHandler implements InvocationHandler { //需要被代理的对象 private Object target; public void setTarget(Object target) { this.target = target; } //执行动态代理对象的所有方法时,都会被替换成执行如下的invoke方法 public Object invoke(Object proxy, Method method, Object[] args) throws Exception { TxUtil tx = new TxUtil(); //执行TxUtil对象中的beginTx。 tx.beginTx(); //以target作为主调来执行method方法 Object result = method.invoke(target , args); //执行TxUtil对象中的endTx。 tx.endTx(); return result; } }
import java.lang.reflect.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class MyInvokationHandler implements InvocationHandler { //需要被代理的对象 private Object target; public void setTarget(Object target) { this.target = target; } //执行动态代理对象的所有方法时,都会被替换成执行如下的invoke方法 public Object invoke(Object proxy, Method method, Object[] args) throws Exception { TxUtil tx = new TxUtil(); //执行TxUtil对象中的beginTx。 tx.beginTx(); //以target作为主调来执行method方法 Object result = method.invoke(target , args); //执行TxUtil对象中的endTx。 tx.endTx(); return result; } }
import java.lang.reflect.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class MyProxyFactory { //为指定target生成动态代理对象 public static Object getProxy(Object target) throws Exception { //创建一个MyInvokationHandler对象 MyInvokationHandler handler = new MyInvokationHandler(); //为MyInvokationHandler设置target对象 handler.setTarget(target); //创建、并返回一个动态代理 return Proxy.newProxyInstance(target.getClass().getClassLoader() , target.getClass().getInterfaces(), handler); } }
/** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class Test { public static void main(String[] args) throws Exception { //创建一个原始的GunDog对象,作为target Dog target = new GunDog(); //以指定的target来创建动态代理 Dog dog = (Dog)MyProxyFactory.getProxy(target); //调用代理对象的info()和run()方法 dog.info(); dog.run(); } }
时间: 2024-11-01 08:31:28