293
技術社區[雲棲]
【設計模式】【動態代理,在方法前和方法後加事務,AOP】
/**
* Description:
* <br/>網站: <a href="https://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="https://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="https://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="https://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="https://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="https://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();
}
}
最後更新:2017-04-04 07:03:38