google / google/guice

Original initialized instance values get lost after gucie injection

Open
#746 1 comment 0 reactions 0 assignees View on GitHub
imported
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

_From [dave.chen80](https://code.google.com/u/106819488827323014470/) on April 25, 2013 04:50:52_

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);
        }
    }
1. Define the interceptor business logic
public class LogInterceptor
        implements MethodInterceptor
{
//business logic here
}
2. Create LogService class
Public class LogService
{
 Injector injector = Guice.createInjector(new InterceptorModule());
}
3. 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>
1. 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.

_Original issue: http://code.google.com/p/google-guice/issues/detail?id=746_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.