Reputation: 363
I have written a MethodInterceptor to gather the performance metrics of a Spring based application. Essentially all service classes and some DAOs will be going through this interceptor. My question is, is there a way to disable this interceptor during runtime to save any performance impact due to call via reflection.
Upvotes: 1
Views: 3006
Reputation: 8169
I don't believe there is a significant performance penalty when using reflection in modern JVM. Also I don't think there is an easy way to disable interceptor dynamically.
If you're doing some non-trivial processing in your interceptor, which you would want to avoid, the easiest way perhaps be to check some property, which could be set on runtime, in your interceptor. Something like this should work:
public abstract class BaseInterceptor implements MethodInterceptor {
private boolean bypass;
/**
* If set to true all processing defined in child class will be bypassed
* This could be useful if advice should have flexibility of being turned ON/OFF via config file
* */
public void setBypass(boolean bypass) {
this.bypass = bypass;
}
public final Object invoke(MethodInvocation methodInvocation) throws Throwable {
if (bypass) {
return methodInvocation.proceed();
}
this.logger.debug(">>>");
return onInvoke(methodInvocation);
}
protected abstract Object onInvoke(MethodInvocation methodInvocation) throws Throwable;
}
In your Spring context file you could set 'bypass' property based on Java system property, for example, or read it from config file.
Upvotes: 2