如何调用带有参数作为注释的方法
可能的重复:
如何使方法被称为被动方法在调用方法之前
例如:
public class Robot{
public static void doSomethingBefore(){
System.out.println("Do something before sayHello");
}
}
public class Person {
@MethodListener(className="Robot",methodName="doSomethingBefore")
public void sayHello(){
System.out.println("hello");
}
public static void main(String[] args){
new Person().sayHello();
}
}
如果我使用注释这样做:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MethodListener {
public String className();
public String methodName();
}
输出将是:
在 sayHello 之前执行一些操作 你好,
现在如果我想在 doSomethingBefore 方法中进行一些更改:
public class Robot{
public static void doSomethingBefore(String name){
System.out.println("Do something before sayHello "+name);
}
}
注释定义应该是什么样子,即 MethodListener 注释中需要进行哪些更改? 请告诉我... 提前致谢
Possible Duplicate:
How to make a method be called passive before invoke a method
for example:
public class Robot{
public static void doSomethingBefore(){
System.out.println("Do something before sayHello");
}
}
public class Person {
@MethodListener(className="Robot",methodName="doSomethingBefore")
public void sayHello(){
System.out.println("hello");
}
public static void main(String[] args){
new Person().sayHello();
}
}
If I do like this using the annotation :
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MethodListener {
public String className();
public String methodName();
}
the output will be:
Do something before sayHello
hello
now if I want to make some changes in doSomethingBefore method :
public class Robot{
public static void doSomethingBefore(String name){
System.out.println("Do something before sayHello "+name);
}
}
how should the Annotation definition look like i. e. what changes are needed in MethodListener Annotation?
Please let me know...
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现这与这个问题严格相关 如何在调用方法之前使方法被动调用 甚至示例代码都是相同的。请先阅读这些答案(它会向您解释一些事情),然后添加更具体的问题。
I see that this is strictly connected with this question How to make a method be called passive before invoke a method Even sample code is the same. Please read those answers first (it will explain you some things) and then add more specific question.