如何调用带有参数作为注释的方法

发布于 2024-11-25 21:41:17 字数 1170 浏览 3 评论 0原文

可能的重复:
如何使方法被称为被动方法在调用方法之前

例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在夏天 2024-12-02 21:41:17

我发现这与这个问题严格相关 如何在调用方法之前使方法被动调用 甚至示例代码都是相同的。请先阅读这些答案(它会向您解释一些事情),然后添加更具体的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文