2009/6/19 Matt <[email protected]>

>
> I'm new to Guice, and I'm using injection within an interceptor, with
> requestInjection() in my module.
>
> The interceptor is being called just fine, so my module appears to be
> good.  The problem is that @Inject is always creating a new object
> using the constructor for that object instead of injecting the
> original pre-initialized one.  What am I missing here?
>

Hi Matt, as the main project page says @Inject is the new new ;)

so wherever you use @Inject you should expect to get a new instance,
unless you've used a scoped binding like singleton - in which case you
will get the same instance for that binding (per-injector).

however, it looks like you just want to get the original instance to use in
the AOP call - you can get this via the "MethodInvocation" object that's
passed in to the method interceptor

for example, from
http://aopalliance.sourceforge.net/doc/org/aopalliance/intercept/MethodInterceptor.html

 class TracingInterceptor implements MethodInterceptor {
   Object invoke(MethodInvocation i) throws Throwable {
     System.out.println("method "+i.getMethod()+" is called on
"+i.getThis()+" with args "+i.getArguments());
     Object ret=i.proceed();
     System.out.println("method "+i.getMethod()+" returns "+ret);
     return ret;
   }
 }

so there's no need to inject the original instance, as it's already
available to you

HTH

public class TestInterceptor implements MethodInterceptor {
>  @Inject MyObject obj; // always a newly-created object
>
>  public Object invoke(...) { obj.doSomething(); ... }
> }
>
> public class TestImpl implements Test {
>  @Inject MyObject obj;
>
>  public TestImpl() {
>    Injector injector = Guice.createInjector(new TestModule());
>    obj = injector.getInstance(MyObject.class);
>    obj.init(); // I want access to this already initialized object in
> the interceptor
>  }
> }
>
> Thanks.
>
>
> >
>


-- 
Cheers, Stuart

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to