MethodInterceptor for class extending ebean Model not working

73 views
Skip to first unread message

shishir gowda

unread,
May 15, 2017, 7:30:07 AM5/15/17
to google-guice
I have ebean entity:

    
    @Entity
    public class Company extends Model {
    @Id
         public Long id;
         static void foo {
               Logger.info("in company");
         }
    ...
    }

A guice Module defining the interceptor:

    
    public class Module extends AbstractModule {
        protected void configure() {

            CacheImpl cache = new CacheImpl();
            requestInjection(cache);
            bindInterceptor(subclassesOf(Company.class), any(), cache);

        }
    }

This does not intercept any calls made to Company (foo(), save(),get(), find()..)

When I change bindInterceptor to a class extending Play Controller, it works by intercepting the calls.

Can some one tell me if I am missing anything?

my methodInterceptor:

    @Component
    public class CacheImpl implements MethodInterceptor {

        public Object invoke(MethodInvocation method) throws Throwable{
            Logger.info("class {}",method.getClass().getName());
            if (method.getMethod().getName() == "list") {
                Logger.info("interceptor in list");
            } else {
                Logger.info("in interceptor for  {}", method.getMethod().getName());

            }
            return method.proceed();

        }
    }

   

Thomas Broyer

unread,
May 15, 2017, 7:39:53 AM5/15/17
to google-guice
Are you really using Guice to inject instances of Company into your classes? Guice will only intercept methods on objects that it instantiates itself (see https://github.com/google/guice/wiki/AOP#limitations) If you want broader use of AOP, you'll have to use another tool, such as AspectJ.
Oh, and I see your foo method is 'static', that's not going to work either, as it cannot be overridden.

shishir gowda

unread,
May 15, 2017, 11:27:43 AM5/15/17
to google-guice
No, I am not injecting instances of Company into classes.
I am actually trying intercept methods of Company which is extending Ebean Model.
I have tried AspectJ too, but with little luck.
Ack wrt foo.

I am basically trying to write a cache layer, which uses redis as cache, and ebean as backend, so the plan is to intercept getters/setters of ebean model, and perform cache ops.

Stephan Classen

unread,
May 15, 2017, 11:34:53 AM5/15/17
to google...@googlegroups.com

Be aware that AOP adds overhead onto these operations. I had to revert usage of AOP in a project once where we some of the methods were called very frequent. In most cases it is no problem but if you application will make heavy use of these getters and setters it could become noticeable.

To your concrete problem:

Guice can only intercept method on objects it has created. This means if you want to be able to intercept methods on objects which are of type Company (or a subclass of it) then you must use Guice to create this object. Either by having Guice inject it into your code or by calling injector.getInstance().

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/e7028ea4-0a6f-48ea-9a94-e6fcd75273c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages