JointPoint有很多方法,下面将介绍其常用的一些方法。
getArgs
获取切入点的参数。示范如下:
@Before("myPointCut()")
public void myBefore(JoinPoint joinPoint) {
Object[] objects = joinPoint.getArgs();
System.out.println((String)objects[0]);
System.out.println((Integer)objects[1]);
}
切入点
public void check(String thing, int time) {
System.out.println(thing + "检查了" + time + "次");
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/testAspectj/applicationContext.xml");
CheckBean checkBean = (CheckBean) applicationContext.getBean("checkBean");
checkBean.check("帐户名", 3);
}
后台输出结果如下
帐户名
3
帐户名检查了3次
getSignature
返回包含目标对象信息的Signature对象,你可以通过Signature对象获得目标对象的Class对象,以及名字,修饰符等等,实例代码如下
@Before("myPointCut()")
public void myBefore(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println(signature.getDeclaringType());
System.out.println(Modifier.toString(signature.getModifiers()));
System.out.println(signature.getDeclaringTypeName());
}
public class CheckBean {
public void check(String thing, int time) {
System.out.println(thing + "检查了" + time + "次");
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/testAspectj/applicationContext.xml");
CheckBean checkBean = (CheckBean) applicationContext.getBean("checkBean");
checkBean.check("帐户名", 3);
}
}
运行结果如下
class testAspectj.CheckBean
public
testAspectj.CheckBean
帐户名检查了3次
getKind
返回连接点的类型。实例代码如下
@Before("myPointCut()")
public void myBefore(JoinPoint joinPoint) {
System.out.println(joinPoint.getKind());
}
public class CheckBean {
public void check(String thing, int time) {
System.out.println(thing + "检查了" + time + "次");
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/testAspectj/applicationContext.xml");
CheckBean checkBean = (CheckBean) applicationContext.getBean("checkBean");
checkBean.check("帐户名", 3);
}
}
运行结果如下
method-execution
帐户名检查了3次