Вы пробовали Google Guice ?
Google Guice позволяет вам немного поориентировать аспекты, перехватывая методы.Если это все, что вам нужно сделать, вы можете реализовать MethodInterceptor, который позволит вам переопределять методы во время выполнения.Это действительно удобно для выявления сквозных задач.
Например, допустим, что вы хотите запретить выполнение определенных методов в выходные дни, вы можете пометить их так:
@Property
public class SomeClass {
public Receipt doSometing() {
// Do something
}
}
Определите MethodInterceptor:
public class PropertyInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
// For example prevent the classes annotated with @Property
// from being called on weekends
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 PropertyModule extends AbstractModule {
protected void configure() {
PropertyInterceptor propertyInterceptor = new PropertyInterceptor();
bindInterceptor(Matchers.annotatedWith(Property.class),
Matchers.any(), propertyInterceptor);
}
}