Discussion:
MethodInterceptor for class extending ebean Model not working
shishir gowda
2017-05-15 11:30:07 UTC
Permalink
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();

}
}
--
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/b1b8f28a-ce1f-46cd-94b8-b920b74fb153%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thomas Broyer
2017-05-15 11:39:53 UTC
Permalink
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.
Post by shishir gowda
@Entity
public class Company extends Model {
@Id
public Long id;
static void foo {
Logger.info("in company");
}
...
}
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?
@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();
}
}
--
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/7bba8281-2148-4ec2-9e48-a506c1464864%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
shishir gowda
2017-05-15 15:27:43 UTC
Permalink
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.
Post by Thomas Broyer
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.
Post by shishir gowda
@Entity
public class Company extends Model {
@Id
public Long id;
static void foo {
Logger.info("in company");
}
...
}
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?
@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();
}
}
--
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.
Stephan Classen
2017-05-15 15:34:49 UTC
Permalink
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().
Post by shishir gowda
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.
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
<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.
@Entity
public class Company extends Model {
@Id
public Long id;
static void foo {
Logger.info("in company");
}
...
}
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?
@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();
}
}
--
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
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
<https://groups.google.com/d/msgid/google-guice/e7028ea4-0a6f-48ea-9a94-e6fcd75273c6%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
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/97a37734-23aa-b070-18f8-00f192ca9383%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.
Loading...