您可以使用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()); }}您可以在此页面上看到更多高级用法



