阅读完需:约 2 分钟
JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象. joinpoint.getargs():获取带参方法的参数
1.joinpoint.getargs():获取带参方法的参数
注:就是获取组件中test方法中的参数,如果test方法中有多个参数,那么这个方法机会返回多个参数.想要哪个就通过for循环加上判断来筛选
2.joinpoint.getTarget():.获取他们的目标对象信息
3..joinpoint.getSignature():(signature是信号,标识的意思):获取被增强的方法相关信息.其后续方法有两个
getDeclaringTypeName: 返回方法所在的包名和类名
getname() : 返回方法名
例子:
先写一个需要被拦截的实现类
@Component("carimp")
public class MyCalImp implements MyCal{
public int mul(int i, int j) {
// TODO Auto-generated method stub
System.out.println("我是方法mul");
return i-j;
}
public int sub(int i, int j) {
// TODO Auto-generated method stub
System.out.println("我是方法sub");
return i+j;
}
}
然后再去拦截它
@Component("jisuan")
@Aspect
public class jisuan {
@Pointcut("execution(* sp.*.*(..))")
public void pointcut(){}
@Before("pointcut()")
public void before(JoinPoint joinPoint) {
System.out.println(Arrays.toString(joinPoint.getArgs())+" "+joinPoint.getSignature().getName());
}
@After("pointcut()")
public void after(JoinPoint joinPoint){
System.out.println("the method *** ends!");
}
}
@Before里面就是拦截的同时还获取了参数列表和方法名字!
结果:
public class MyTest {
ApplicationContext context;
@Before
public void fa(){
context=new ClassPathXmlApplicationContext("ctx.xml");
}
@Test
public void t1(){
MyCal c=(MyCal) context.getBean("carimp");
// System.out.println(c.mul(1, 2));
int x=c.mul(1, 5);
System.out.println(x);
int y=c.sub(5, 6);
System.out.println(y);
}
}
