您的位置:首页 > 编程语言 > ASP

org.aspectj.lang.JoinPoint解析

2015-09-14 14:48 996 查看
org.aspectj.lang.JoinPoint接口表示目标类连接点对象,方法可以通过将传入参数声明为JoinPoint的类型来访问到连接点上下文的信息。JoinPoint接口和其子接口ProceedingJoinPoint 的主要方法有: 
1)JoinPoint 
Object[] getArgs():获取连接点方法运行时的传入参列表; 
Signature getSignature() :获取连接点的方法对象; 
Object getTarget() :获取连接点所在的目标对象; 
Object getThis() :获取代理对象本身; 
2)ProceedingJoinPoint 
ProceedingJoinPoint继承JoinPoint子接口,它只是新增了两个用于执行连接点方法的方法: 
 proceed() throws java.lang.Throwable:通过反射执行目标对象的连接点处的方法; 
 proceed(java.lang.Object[] args) throws java.lang.Throwable:通过反射执行目标对象连接点处的方法,使用新的入参替换原来的入参。 

使用介绍:

    private Log logger = LogFactory.getLog(LogAspect.class);  

    public void doSystemLog(JoinPoint point) throws Throwable {   

        Object[] param = point.getArgs();  

        for(int i=0;i<param.length;i++){

        System.out.println("参数"+i+"=: "+param[i]);

       

        }

        Method method = null;  

        String methodName = point.getSignature().getName();  

        System.out.println("签名方法:================"+methodName);

        if (!(methodName.startsWith("set") || methodName.startsWith("get")||methodName.startsWith("query"))){  

            Class targetClass = point.getTarget().getClass();  

            System.out.println("类名:================"+targetClass.getName());

            try {

                method = targetClass.getMethod(methodName, param[0].getClass()); 
} catch (Exception e) {
// TODO: handle exception

System.out.println("类方法:================"+method);

            if (method != null) {   

                boolean hasAnnotation = method.isAnnotationPresent(Log.class);   

                if (hasAnnotation) {  

                Log annotation = method.getAnnotation(Log.class);   

                    String methodDescp = annotation.operationType()+annotation.operationName();  

                    if (logger.isDebugEnabled()) {   

                        logger.debug("Action method:" + method.getName() + " Description:" + methodDescp);   

                    }   

                }   

            }   

        }   

    }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: