Bartek Majsak
2009-06-15 12:10:46 UTC
Hello,
I am involved in the project based on JSF 1.1 (lucky me ;) and I would like
to utilize advantages of dependency injection container to make the code
more testable.
I've configured WebappComposer implementation based on jsf example provided
with the pico-web distribution and it works perfectly fine.
If I understand it correctly it only supports constructor injection and it's
also perfectly fine since I want to have all required collaborators injected
during construction.
However I would like to use additional strategy since not all of the class
dependencies are required. So I choose AnnotatedMethodInjection and
here is the code of my customized version of PicoServletContainerListener
with overridden makeScopedContainers method:
public class CustomizedPicoServletContainerListener extends
PicoServletContainerListener {
private static final long serialVersionUID = -312416248620423239L;
@Override
protected ScopedContainers makeScopedContainers() {
DefaultPicoContainer appCtnr = new DefaultPicoContainer(new
Guarding()
.wrap(new Caching()), makeLifecycleStrategy(),
makeParentContainer(), makeAppComponentMonitor());
Storing sessStoring = new Storing();
DefaultPicoContainer sessCtnr = new DefaultPicoContainer(new
Guarding()
.wrap(sessStoring.wrap(makeCompositeInjection())),
makeLifecycleStrategy(), appCtnr,
makeSessionComponentMonitor());
Storing reqStoring = new Storing();
DefaultPicoContainer reqCtnr = new DefaultPicoContainer(new
Guarding()
.wrap(addRequestBehaviors(reqStoring).wrap(makeCompositeInjection())),
makeLifecycleStrategy(), sessCtnr,
makeRequestComponentMonitor());
return new ScopedContainers(appCtnr, sessCtnr, reqCtnr, sessStoring,
reqStoring);
}
private ComponentFactory makeCompositeInjection() {
return new CompositeInjection(new ConstructorInjection(), new
AnnotatedMethodInjection());
}
}
Unfortunately I've discovered quite strange behaviour (at least for my
understanding).
Here's the scenario:
1. I have two session-scoped managed beans registered in session scoped
container. Let's call them A and B.
2. I'm creating a new session thus also instance of A and injecting
dependencies using constructor injection (but not instance of B) (let's call
this particular instance a1)
3. Then I inject instance of B using annotated method (standard @Inject
annotation) (let's call it b1)
4. I'm creating new session - a2 is created and constructor injection works
perfectly fine (the same as in 2.)
5. When it comes to method injection here what happens: b2 is created but
injected to a1 so b1 is overwritten and a2 still has null.
Looking forward to any advices or clarifications.
Best regards,
Bartosz Majsak
I am involved in the project based on JSF 1.1 (lucky me ;) and I would like
to utilize advantages of dependency injection container to make the code
more testable.
I've configured WebappComposer implementation based on jsf example provided
with the pico-web distribution and it works perfectly fine.
If I understand it correctly it only supports constructor injection and it's
also perfectly fine since I want to have all required collaborators injected
during construction.
However I would like to use additional strategy since not all of the class
dependencies are required. So I choose AnnotatedMethodInjection and
here is the code of my customized version of PicoServletContainerListener
with overridden makeScopedContainers method:
public class CustomizedPicoServletContainerListener extends
PicoServletContainerListener {
private static final long serialVersionUID = -312416248620423239L;
@Override
protected ScopedContainers makeScopedContainers() {
DefaultPicoContainer appCtnr = new DefaultPicoContainer(new
Guarding()
.wrap(new Caching()), makeLifecycleStrategy(),
makeParentContainer(), makeAppComponentMonitor());
Storing sessStoring = new Storing();
DefaultPicoContainer sessCtnr = new DefaultPicoContainer(new
Guarding()
.wrap(sessStoring.wrap(makeCompositeInjection())),
makeLifecycleStrategy(), appCtnr,
makeSessionComponentMonitor());
Storing reqStoring = new Storing();
DefaultPicoContainer reqCtnr = new DefaultPicoContainer(new
Guarding()
.wrap(addRequestBehaviors(reqStoring).wrap(makeCompositeInjection())),
makeLifecycleStrategy(), sessCtnr,
makeRequestComponentMonitor());
return new ScopedContainers(appCtnr, sessCtnr, reqCtnr, sessStoring,
reqStoring);
}
private ComponentFactory makeCompositeInjection() {
return new CompositeInjection(new ConstructorInjection(), new
AnnotatedMethodInjection());
}
}
Unfortunately I've discovered quite strange behaviour (at least for my
understanding).
Here's the scenario:
1. I have two session-scoped managed beans registered in session scoped
container. Let's call them A and B.
2. I'm creating a new session thus also instance of A and injecting
dependencies using constructor injection (but not instance of B) (let's call
this particular instance a1)
3. Then I inject instance of B using annotated method (standard @Inject
annotation) (let's call it b1)
4. I'm creating new session - a2 is created and constructor injection works
perfectly fine (the same as in 2.)
5. When it comes to method injection here what happens: b2 is created but
injected to a1 so b1 is overwritten and a2 still has null.
Looking forward to any advices or clarifications.
Best regards,
Bartosz Majsak