[英]How to add “after” method in AspectJ?
I'm trying to understand how AspectJ works. 我试图了解AspectJ的工作原理。 To begin I want to add a method called
after
, calling usual existing using AspectJ. 首先,我想添加一个名为
after
的方法,使用AspectJ调用通常存在的方法。
myClass can be seen below: myClass可以在下面看到:
package myclass;
public class MyClass {
public void method1(){
System.out.println("methode1 is called");
}
public void method2(String str){
System.out.println("methode2 is called " + str);
}
}
After method2 I want to show some text message and argument str
: 在method2之后,我想显示一些文本消息和参数
str
:
package myclass;
public aspect aspect2 {
pointcut func(String str):
call(myclass.MyClass.method2(String)) && args(str);
after(String str) : func(str) {
System.out.println("Aspect from method2: " + str );
}
}
Finally to call it there is main class entrypoint
: 最后调用它是主类
entrypoint
:
package myclass;
public class entrypoint {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyClass mc = new MyClass();
mc.method1();
mc.method2("zzz");
}
}
As an input, it shows : 作为输入,它显示:
methode1 is called
methode2 is called zzz
It does not show Aspect from method2: zzz
, so aspect doesn't work. 它没有显示
Aspect from method2: zzz
,所以Aspect from method2: zzz
不起作用。 What am I doing wrong? 我究竟做错了什么?
I post my comment as an answer as @kriegaex ask. 我以@kriegaex的要求发布评论作为答案。 You have some mistake in the code: 1. In the pointcut you forgot to add the return type even if it's void you should specified it.
您在代码中有一些错误:1.在切入点中,您忘记添加返回类型,即使该返回类型为空,也应该指定它。 2. In the advice add returning
after(String str) returning : func(str)
2.在建议中,在返回
after(String str) returning : func(str)
添加after(String str) returning : func(str)
package myclass;
public aspect aspect2 {
pointcut func(String str): call(void myclass.MyClass.method2(String)) && args(str);
after(String str) returning : func(str) {
System.out.println("Aspect from method2: " + str );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.