基于CGLib(面向Class)的AOP的简单实现.

/*
 *auth: lanmh
 */

/**
 * 基于CGLib(面向Class)的AOP的简单实现.
 */

import java.lang.reflect.*;
import java.util.*;

import net.sf.cglib.proxy.*;
import org.apache.commons.logging.Log;

public abstract class AOPBaseClass
    implements MethodInterceptor
{
    private static Log logger = org.apache.commons.logging.LogFactory.getLog(
        AOPBaseClass.class);

    /**用于动态产生被代理类的子类*/
    private Enhancer enhancer = new net.sf.cglib.proxy.Enhancer();

    /**存放所有的拦截器**/
    protected List interceptors = new ArrayList();

    /**要拦截方法的名称集合*/
    protected List interMethodNames = new ArrayList();

    public AOPBaseClass()
    {
        initInterceptors(); //初始化拦截器
        initInterMethodNames(); //初始化拦截方法名称集合
    }

    /***
     * 初始化拦截器
     */
    protected abstract void initInterceptors();

    /**
     * 初始化拦截的方法的名称集合
     */
    protected abstract void initInterMethodNames();

    /**
     *动态产生被代理类的子类的实例..
     * @param clz Class被代理类的Class
     * @return Object 返回被代理类的子类的实例
     */
    public Object bind(Class clz)
    {
        enhancer.setSuperclass(clz); //设置超类
        enhancer.setCallback(this); //设置回调
        Object obj = enhancer.create(); //生成被代理类的子类实例
        return obj;
    }

    /**
     *对被代理的类的方法进行拦截
     * @param proxy Object 动态生成的被代理类的子类
     * @param method Method 被代理类被拦截的方法
     * @param methodParameters Object[] 被代理类被拦截的方法的参数列表
     * @param methodProxy MethodProxy  动态生成的被代理类的子类的方法
     * @throws Throwable
     * @return Object 返回被代理类被拦截的方法执行的结果
     */
    public Object intercept(Object proxy, Method method,
                            Object[] methodParameters, MethodProxy methodProxy)
        throws Throwable

    {
        Object result = null;
        InvocationInfo invInfo = null;
        Throwable ex = null;

        String methodName = method.getName();
        invInfo = new InvocationInfo(proxy,
                                     method,
                                     methodParameters,
                                     methodProxy,
                                     result,
                                     ex);

        if (interMethodNames.contains(methodName)) //如果调用的方法名称存在于拦截方法的名称集合
        {
            try
            {

                //调用 Before Intercetpors
                invokeInterceptorsBefore(invInfo);

                result = methodProxy.invokeSuper(proxy, methodParameters);

                //调用  After Intercetpors
                invInfo.setResult(result);
                invokeInterceptorsAfter(invInfo);
            }
            catch (Throwable tr)
            {
                //调用 Exception Intercetpors
                invInfo.setException(tr);
                invokeInterceptorsExceptionThrow(invInfo);
                throw tr;
            }
            finally
            {
                //调用last Intercetpors
                invokeInterceptorsLast(invInfo);
            }
        }
        else //否则如果调用的方法名称存在于拦截方法的名称集合
        {
            //直接调用被代理类的方法
            result = methodProxy.invokeSuper(proxy, methodParameters);
        }
        return result;

    }

    /**
     * 调用Before Interceptor
     * @param invInfo InvocationInfo
     */
    private void invokeInterceptorsBefore(InvocationInfo invInfo) throws EduException
    {

        //递增循环调用所有拦截器(interceptors)的before方法;
        for (int i = 0; i < interceptors.size(); i++)
        {
            Interceptor brt = (Interceptor) interceptors.get(i);
            brt.before(invInfo);
        }
    }

    /**
     * 调用After Interceptor
     * @param invInfo InvocationInfo
     */
    private void invokeInterceptorsAfter(InvocationInfo invInfo)throws EduException
    {

        //递减循环调用所有拦截器(interceptors)的After方法;
        for (int i = interceptors.size() - 1; i >= 0; i--)
        {
            Interceptor brt = (Interceptor) interceptors.get(i);
            brt.after(invInfo);
        }
    }

    /**
     * 调用Last Interceptor
     * @param invInfo InvocationInfo
     */
    private void invokeInterceptorsLast(InvocationInfo invInfo)throws EduException
    {

        //递减循环调用所有拦截器(interceptors)的After方法;
        for (int i = interceptors.size() - 1; i >= 0; i--)
        {
            Interceptor brt = (Interceptor) interceptors.get(i);
            brt.last(invInfo);
        }
    }

    /**
     * 调用exceptionThrow Interceptor
     * @param invInfo InvocationInfo
     */
    private void invokeInterceptorsExceptionThrow(InvocationInfo invInfo)throws EduException
    {

        //递减循环调用所有拦截器(interceptors)的exceptionThrow方法;
        for (int i = interceptors.size() - 1; i >= 0; i--)
        {
            Interceptor brt = (Interceptor) interceptors.get(i);
            brt.exceptionThrow(invInfo);
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值