aspect学习(2)target&thisJoinPoint

       在上一篇文章中我们学习了aspect的before/after/around的基础,现在接着学习,仍然是需求驱动学习,接着上一篇。


需求三

    在需求一的基础上增加,当调用setX,setY,setZ方法时,打印属性x,y,z改变前后的值。


     想要知道x,y,z改变前的值,那么在LogAspect.aj中必须要能拿到目标对象的实例、当前调用的是目标对象中的哪个方法(为什么只需知道这2个东东就可以了呢?大家可以先想想。。。。)

    那么该怎样拿到目标对象实例呢?看代码

LogAspect.aj的代码

package com.fei.aspect;


import org.aspectj.lang.Signature;

import com.fei.bean.Point;

public aspect LogAspect {

	public pointcut printLog(Object p): call( void Point.set*(int)) && target(p);
	
	void around(Object p):printLog(p){
		System.out.println(p);
		System.out.println(thisJoinPoint.getTarget());
		System.out.println(thisJoinPoint.getKind());
		for(Object o : thisJoinPoint.getArgs())
			System.out.println(o);
		System.out.println( "signature========");
		Signature signature = thisJoinPoint.getSignature();
		System.out.println(signature.getClass());
		System.out.println(signature.getName());
		System.out.println(signature.getDeclaringTypeName());
		System.out.println(signature.getModifiers());
		System.out.println(signature.toLongString());
	
	}
	
	
}

MainTest.java源码及运行结果截图



     通过上面源码及结果我们知道,要想拿到目标对象实例,有2中途径,一是在 pointcut中用target,二是直接用thisJoinPoint.getTarget()拿到,方法名可用signature.getName()拿到。


   好了,我们需要的东东可以拿到了,下面源码来实现需求。

package com.fei.aspect;


import java.lang.reflect.Method;

import org.aspectj.lang.Signature;

import com.fei.bean.Point;

public aspect LogAspect {

	public pointcut printLog(): call( void Point.set*(int));
	
	void around():printLog(){
		Object o = thisJoinPoint.getTarget();
		Signature signature = thisJoinPoint.getSignature();
		String methodName = signature.getName();
	//	System.out.println("在调用"+signature.getDeclaringTypeName()+"."+signature.getName());
		System.out.println("在调用"+signature.toLongString());
		//通过反射拿值
		System.out.println("属性改变前:" + getXXX(o,methodName));
		proceed();
		//通过反射拿值
		System.out.println("属性改变后:" + getXXX(o,methodName));
	
	}
	
	private Object getXXX(Object o, String methodName){
		Object value = null;
		try {
			Method m = o.getClass().getMethod(methodName.replace("set", "get"), null);
			value = m.invoke(o, null);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return value;
	}
	
}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值