您可以使用AspectJ,也可以使用带有自己的AOP的Google Guice。
具有注解方法注解的对象WaitCursor
必须注入Guice。
您定义注释
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface WaitCursor {}
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());
}
}