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

news/2024/6/19 0:31:04 标签: AOP, Apache, .net

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

}


http://www.niftyadmin.cn/n/1552160.html

相关文章

pthread_create源码分析

pthread_create源码分析 下面来看glibc中pthread_create函数的源码&#xff0c;分为两部分来看。 __pthread_create_2_1第一部分 nptl/pthread_create.c int __pthread_create_2_1 (newthread, attr, start_routine, arg)pthread_t *newthread;const pthread_attr_t *attr;…

Eclipse快速上手指南之使用ANT

Ant是Java平台下非常棒的批处理命令执行程序&#xff0c;能非常方便地自动完成编译&#xff0c;测试&#xff0c;打包&#xff0c;部署等等一系列任务&#xff0c;大大提高开发效率。如果你现在还没有开始使用Ant&#xff0c;那就要赶快开始学习使用&#xff0c;使自己的开发水…

__pthread_initialize_minimal源码分析

__pthread_initialize_minimal源码分析 __pthread_initialize_minimal在__libc_start_main中被调用&#xff0c;下面来看 glibc nptl/nptl-init.c void __pthread_initialize_minimal_internal (void) {__libc_setup_tls (TLS_TCB_SIZE, TLS_TCB_ALIGN);struct pthread *pd …

VMware vCloud 解决方案

什么是云计算&#xff1f; 云计算是指使用联网的基础架构软件和容量以按需方式向用户提供资源的计算方式。采用云计算时&#xff0c;信息将永久存储在 Internet 上的服务器中&#xff0c;同时还将临时缓存在桌面计算机、笔记本电脑、手持设备或其他客户端设备上。在这一通常称…

sys_nice源码分析

sys_nice源码分析 sys_nice系统调用用于改变进程的优先级&#xff0c;下面来看。 sys_nice kernel/sched/core.c SYSCALL_DEFINE1(nice, int, increment) {long nice, retval;increment clamp(increment, -NICE_WIDTH, NICE_WIDTH);nice task_nice(current) increment;n…

数据中心如何向私有云转变

数据中心作为企业IT基础设施的核心部分&#xff0c;在不断发展的过程中也不断面临着来自管理繁复、成本高昂、资源利用率低等方面的问题。几年前突然蹦出的云计算概念让传统数据中心看到了希望&#xff0c;如果能灵活高效的整合自身资源&#xff0c;并转而以“将IT当作服务进行…

jdbc 隔离级别

在通过JDBC对数据库进行并发访问时&#xff0c;为了解决并发之间的锁的控制&#xff0c;JDBC提供了一个隔离级别&#xff08;Isolation&#xff09;的方式解决并发访问的问题。 因为最近时间在解决公司工作流在客户现场的高并发情况下经常出现死锁&#xff08;dead lock&#x…

虚拟化如何更好的服务企业私有云?

摘要&#xff1a;随着云的热度升高&#xff0c;也使得虚拟化技术更加炙手可热。而在实现云应用的过程中&#xff0c;特别是私有云&#xff0c;虚拟化是其中的关键技术。那么&#xff0c;到底什么算是私有云呢&#xff1f;私有云则是指一个企业单独使用而构建的环境&#xff0c;…