现在的位置: 首页 > 综合 > 正文

Spring AOP:访问目标方法的参数

2013年10月04日 ⁄ 综合 ⁄ 共 3247字 ⁄ 字号 评论关闭

访问目标方法 最简单的做法就是定义增强处理方法时将第一个参数定义为JoinPoint
类型,当该增强处理方法被调用时,该JoinPoint参数就代表了织入增强处理的连接点。JoinPoint里包含了如下几个常用的方法:

Object[ ] getArgs( ) 返回执行目标方法时的参数
Signature getSignature( ) 返回被增强的方法的相关信息
Object getTarget( ) 返回被织入增强处理的目标对象
Object getThis( ) 返回AOP框架为目标对象生成的代理对象

Person.java :

public interface Person {
	public String sayHello(String name);
	public void eat(String food);
}

Chinese.java :

@Component
public class Chinese implements Person {

	@Override
	public String sayHello(String name) {
		System.out.println("sayHello方法被调用...");
		return name+" Hello,Spring AOP";
	}

	@Override
	public void eat(String food) {
		System.out.println("我正在吃:"+food);
	}

	
}

FourAdviceTest.java :

@Aspect
public class FourAdviceTest {

	@Around("execution(* com.bean.*.*(..))")
	public Object processTx(ProceedingJoinPoint jp) throws Throwable{
		System.out.println("Around增强:执行目标方法之前,模拟开始事务...");
		Object[] args=jp.getArgs();
		if(args!=null && args.length>0 && args[0].getClass()==String.class){
			args[0]="被改变的参数";
		}
		Object rvt=jp.proceed(args);
		System.out.println("Around增强:执行目标方法之后,模拟结束事务...");
		return rvt+" 新增的内容";
	}
	
	@Before("execution(* com.bean.*.*(..))")
	public void authority(JoinPoint jp){
		System.out.println("Before增强:模拟执行权限检查...");
		System.out.println("Before增强:被织入增强处理的目标方法为:"+
				jp.getSignature().getName());
		System.out.println("Before增强:目标方法的参数为:"+Arrays.toString(jp.getArgs()));
		System.out.println("Before增强:被织入增强处理的目标对象为:"+jp.getTarget());
	}
	
	@AfterReturning(returning="rvt",pointcut="execution(* com.bean.*.*(..))")
	public void log(JoinPoint jp,Object rvt){
		System.out.println("AfterReturning增强:获取目标方法返回值:"+rvt);
		System.out.println("AfterReturning增强:模拟记录日志功能...");
		System.out.println("AfterReturning增强:被织入增强处理的目标方法为:"+
				jp.getSignature().getName());
		System.out.println("AfterReturning增强:目标方法的参数为:"+
				Arrays.toString(jp.getArgs()));
		System.out.println("AfterReturning增强:被织入增强处理的目标对象为:"+
				jp.getTarget());
	}
	
	@After("execution(* com.bean.*.*(..))")
	public void release(JoinPoint jp){
		System.out.println("After增强:模拟方法结束后的释放资源...");
		System.out.println("After增强:被织入增强处理的目标方法为:"+
				jp.getSignature().getName());
		System.out.println("After增强:目标方法的参数为:"+
				Arrays.toString(jp.getArgs()));
		System.out.println("After增强: 被织入增强处理的目标对象为:"+
				jp.getTarget());
	}
}

bean.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-2.5.xsd


http://www.springframework.org/schema/tx


http://www.springframework.org/schema/tx/spring-tx-2.5.xsd


http://www.springframework.org/schema/aop

                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
    
    <context:component-scan base-package="com.bean">
        <context:include-filter type="annotation" 
                 expression="org.aspectj.lang.annotation.Aspect"/>
    </context:component-scan>
    
    <aop:aspectj-autoproxy/>
    
 </beans>

Test.java :

public class Test {
	public static void main(String[] args) {
		
		ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
		Person p=(Person) ctx.getBean("chinese");
		System.out.println(p.sayHello("张三"));
		p.eat("西瓜");
	}
}

运行程序,控制台输出:

抱歉!评论已关闭.