java Java注解在方法之前和之后执行一些代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12195015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:59:22  来源:igfitidea点击:

Java annotation to execute some code before and after method

javaswingannotationscursor

提问by Stan

I'm writing an swing app and i'd like to have 'wait' cursor when some methods are executed. We can do it this way:

我正在编写一个 Swing 应用程序,我希望在执行某些方法时有“等待”光标。我们可以这样做:

public void someMethod() {
    MainUI.getInstance().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    //method code
    MainUI.getInstance().setCursor(Cursor.getDefaultCursor());
}

What I'd like to achieve is a java annotation, that would set wait cursor before method execution and set it back to normal after execution. So previous example would look something like this

我想要实现的是一个 java 注释,它会在方法执行之前设置等待光标,并在执行后将其设置为正常。所以前面的例子看起来像这样

@WaitCursor    
public void someMethod() {
    //method code
}

How can i achieve this? Suggestions about other variants of solving this problem are also welcome. Thanks!

我怎样才能做到这一点?也欢迎提出有关解决此问题的其他变体的建议。谢谢!

P.S. - We use Google Guice in our project, but i don't get how to solve the problem using it. If someone would provide me with simple example of similar problem, that would be very helpful

PS - 我们在我们的项目中使用 Google Guice,但我不知道如何使用它来解决问题。如果有人能为我提供类似问题的简单示例,那将非常有帮助

回答by Alex

You may use AspectJ, or use Google Guice which bring its own AOP.

您可以使用 AspectJ,或者使用自带 AOP 的 Google Guice。

The object having the method annotated with your WaitCursorannotation must be injected with Guice.

使用您的WaitCursor注解来注解方法的对象必须注入 Guice。

You define your annotation

你定义你的注释

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface WaitCursor {}

You add a MethodInterceptor :

你添加一个 MethodInterceptor :

public class WaitCursorInterceptor implements MethodInterceptor {
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // show the cursor
        MainUI.getInstance().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        // execute the method annotated with `@WaitCursor`
        Object result = invocation.proceed();
        // hide the waiting cursor
        MainUI.getInstance().setCursor(Cursor.getDefaultCursor());
        return result;
    }
}

And define a module where you bind the interceptor on any method having your annotation.

并定义一个模块,您可以在其中将拦截器绑定到具有注释的任何方法上。

public class WaitCursorModule extends AbstractModule {
    protected void configure() {
        bindInterceptor(Matchers.any(), Matchers.annotatedWith(WaitCursor.class), new WaitCursorInterceptor());
    }
}

You can see more advanced uses on this page

您可以在此页面上查看更多高级用法

回答by Vikdor

You might want to look at using around() advice in AspectJin conjunction with your annotation to associate the around() advice with all methods that are qualified with your annotation.

您可能希望在 AspectJ中结合使用around() 建议和您的注释来将 around() 建议与您的注释限定的所有方法相关联。