几段代码让你明白AOP和struts2中拦截器的原理

代码是李刚老师书上的源码,有助于我们理解动态代理的原理。

Dog.java
package lee;

/*
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public interface Dog
{
public void info();

public void run();
}

DogImpl.java
package lee;


/*
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class DogImpl implements Dog
{
public void info()
{
System.out.println("我是一只猎狗");
}
public void run()
{
System.out.println("我奔跑迅速");
}
}

package lee;

/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class DogIntercepter
{
public void method1()
{
System.out.println("=====模拟通用方法一=====");
}

public void method2()
{
System.out.println("=====模拟通用方法二=====");
}
}

package lee;

import java.lang.reflect.*;
/*
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class MyProxyFactory
{
/**
* 实例Service对象。
* @param serviceName String
* @return Object
*/
public static Object getProxy(Object object)
{
//Dog控制类 代理的操作类
ProxyHandler handler = new ProxyHandler();
//把该dog实例托付给代理操作
handler.setTarget(object);
//第一个参数是用来创建 动态代理 的ClassLoader对象,只要该对象能访问Dog接口即可。
//也就只要DogImpl与Dog在同一
return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(),handler);
}
}

package lee;

import java.lang.reflect.*;
import java.util.*;
/*
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/

public class ProxyHandler implements InvocationHandler
{
private Object target;
DogIntercepter di = new DogIntercepter();
public Object invoke(Object proxy, Method method, Object[] args)throws Exception
{
Object result = null;
if (method.getName().equals("info"))
{
di.method1();
result =method.invoke(target, args);
di.method2();
}
else
{
result =method.invoke(target, args);
}
return result;
}
public void setTarget(Object o)
{
this.target = o;
}
}

package lee;

/*
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class TestDog
{
public static void main(String[] args)
{
Dog targetObject = new DogImpl();
Dog dog = null;
Object proxy = MyProxyFactory.getProxy(targetObject);
if (proxy instanceof Dog)
{
dog = (Dog)proxy;
}
dog.info();
dog.run();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值