<span style="font-size:14px;">@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)//标注于方法上 @interface NotOnWeekends {}</span>
1. 编写一注解
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)//标注于方法上 @interface NotOnWeekends {}
public class RealBillingService implements BillingService { @NotOnWeekends public Receipt chargeOrder(PizzaOrder order, CreditCard creditCard) { ... } }
public class WeekendBlocker implements MethodInterceptor { public Object invoke(MethodInvocation invocation) throws Throwable { Calendar today = new GregorianCalendar(); if (today.getDisplayName(DAY_OF_WEEK, LONG, ENGLISH).startsWith("S")) { throw new IllegalStateException( invocation.getMethod().getName() + " not allowed on weekends!"); } return invocation.proceed(); } }
public class NotOnWeekendsModule extends AbstractModule { protected void configure() { bindInterceptor(Matchers.any(), Matchers.annotatedWith(NotOnWeekends.class), new WeekendBlocker()); } }
public class NotOnWeekendsModule extends AbstractModule { protected void configure() { WeekendBlocker weekendBlocker = new WeekendBlocker(); requestInjection(weekendBlocker); bindInterceptor(Matchers.any(), Matchers.annotatedWith(NotOnWeekends.class), weekendBlocker); } }
public class NotOnWeekendsModule extends AbstractModule { protected void configure() { bindInterceptor(any(), annotatedWith(NotOnWeekends.class), new WeekendBlocker(getProvider(Calendar.class))); } }
原文地址:http://blog.csdn.net/xtayfjpk/article/details/40748981