【设计模式】【动态代理,在方法前和方法后加事务,AOP】


/**
 * 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();
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值