I'm trying to integrate Guice into a JSF 1.2 (Sun RI) application, and I want to be able to do the following to my managed-beans:
- Inject dependencies using the Guice
@Inject
annotation, then - Perform initialisation using the
@PostConstruct
annotation
My problem is that the @PostConstruct
method is always invoked before the @Inject
annotations are processed. Does anyone know of a solution to this problem?
The managed-bean:
public final class Foo {
@Inject private BazService bazService;
private Baz baz;
@PostConstruct
public void init() {
bar = bazService.loadBaz();
}
public void setBazService(BazService bazService) {
this.bazService = bazService;
}
}
The managed-bean declaration:
<managed-bean>
<managed-bean-name>foo</managed-bean-name>
<managed-bean-class>bean.Foo</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>id</property-name>
<value>#{param.id}</value>
</managed-property>
</managed-bean>
The Guice bindings:
public final class MyGuiceModule extends AbstractModule {
@Override
protected void configure() {
bind(BazService.class).to(DummyBazService.class).in(Scopes.SINGLETON);
}
}
I've tried the following:
- manually invoking
Injector.injectMembers(Object)
(@PostConstruct
isn't processed in superclass) - using a custom
VariableResolver
(@PostConstruct
is processed before@Inject
) - using MyFaces'
GuiceResolver
(just did not appear to work at all - perhaps something to do with this issue)
I'm happy to consider other options if this appears to be the wrong way of approaching things... Any help appreciated.
Edit
I was using Guice 1.0. I've now upgraded to Guice 2.0 but the problem remains. I've found some discussion that seems to relate to my issue... but I don't understand how to use this information :(