Status: New
Owner: ----

New issue 746 by [email protected]: Original initialized instance values get lost after gucie injection
http://code.google.com/p/google-guice/issues/detail?id=746

Description of the issue:

I use Gucie to intercept any methods that have my defined annotation @LogRequired. However for my application, some beans are initialized by Spring with injected fields values. After calling giuce injector.injectMembers(this), the beans gets proxied by guice but all original fields values are gone. Looks like Guice re-constucts the beans and throw away all old values. Is this expected behavior or how can I solve this issue?

Steps to reproduce:
1. Create a class extends AbstractModule
public class InterceptorModule extends AbstractModule{
    public void configure()
    {
            LogInterceptor tracing = new LogInterceptor();
            requestInjection(tracing);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(LogRequired.class), tracing);
        }
    }

2. Define the interceptor business logic
public class LogInterceptor
        implements MethodInterceptor
{
//business logic here
}

3. Create LogService class
Public class LogService
{
 Injector injector = Guice.createInjector(new InterceptorModule());
}

4. I have one of the bean example below with the getName method wants to be intercepted:
public class UserImplTwo implements IUser
{

        private String name;
        

        @LogRequired
        public String getName() {
                return this.name;
        }

        public void setName(String name) {
                this.name = name;
        }

}

which is initialized  by Spring context:
    <bean id="userTwo" class="com.consumer.impl.UserImplTwo">
        <property name="name" value="hello world"></property>
    </bean>

5. Finally I have a consumer to consume the bean:
public class Consumer
{
        @Inject
        private UserImplTwo instance;

        public void setInstance(UserImplTwo instance)
        {
            this.instance = instance;
        }

      public void init()
        {
           // the value of name is printed out as 'hello world'
             System.out.println(  this.instance.getName());

             LogService.injector.injectMembers(this);


// the value of name is printed out as null, should be 'hello world'
             System.out.println(  this.instance.getName());

        }

Then use Spring to initialized the bean:

     <bean id="consumer" class="com.demo.Consumer" init-method="init">
        <property name="instance" ref="userTwo"></property>
    </bean>


Please let me know if this the the right approach or if I did something wrong, because I have to use Spring to initialize some beans.

--
You received this message because this project is configured to send all issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to