JDK proxy can't extend classes, you would need to Create a CGLib proxy. But I would recommend using this as the last resort when all your hopes with JDK proxy have ran out.
Below is a sample code to create a CGLib proxy.
Don't judge on the code as I had to remove lots of code before posting copyrighted code.
User trace= (User) Enhancer.create(User.class, new MethodInterceptor(){
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
long start = System.currentTimeMillis();
try{
return method.invoke(user, args);
} finally {
logger.debug(" TimeLog: Time taken in "+ (System.currentTimeMillis() - start));
}
}
});
Below is a sample code to create a CGLib proxy.
Don't judge on the code as I had to remove lots of code before posting copyrighted code.
User trace= (User) Enhancer.create(User.class, new MethodInterceptor(){
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
long start = System.currentTimeMillis();
try{
return method.invoke(user, args);
} finally {
logger.debug(" TimeLog: Time taken in "+ (System.currentTimeMillis() - start));
}
}
});
Comments
Post a Comment