0

I use Gucie 3.0 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?

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); } }

Define the interceptor business logic

public class LogInterceptor implements MethodInterceptor { //business logic here }

Create LogService class

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

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:

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.

4

1 回答 1

0

如果您使用 Spring Framework,则“正确的方法”可能是保持简单并使用 Spring 的 DI,而不是尝试与 Guice 混合搭配 :-)

话虽如此,似乎没有技术上的原因为什么它们不能在某种程度上混合和匹配在一起。

我认为你会用另一种方法取得更大的成功。我之前使用过的一种是使用 Spring MVC 基于 Java 的配置。这是基本方法。

创建一个扩展 WebMvcConfigurationSupport 的类:

@Configuration
@Import(BeansConfig.class)
public class Config extends WebMvcConfigurationSupport {
}

分离出你的 bean 配置(可能它可以与上面的内容合并,但我想这是相当枯燥的代码,你通常不想看到它)。在将它们提供给 Spring 之前,使用它使用 Guice 注入器创建 bean。

@Configuration
public class BeansConfig {
    @Bean
    public Consumer getConsumer() {
        return SomeGuiceInjectorFactory.newInstance(Consumer.class);
    }
}

将其包含在您的 spring.xml 中(或者如果您的 servlet 容器比我的更新,则以其他方式引导)

<context:annotation-config/>
<bean id="extendedWebMvcConfig" class="Config"/>

构造函数注入和大多数/全部?其他 Guice 的优点也应该适用于这种情况。

此外,您不需要在 xml 中配置您的 bean。

于 2013-04-26T21:07:22.417 回答