栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

您可以使用AspectJ,也可以使用带有自己的AOP的Google Guice。

具有注解方法注解的对象

WaitCursor
必须注入Guice。

您定义注释

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

您添加一个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;    }}

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

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

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569681.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号