Reputation: 8587
I've got my application setup to use CDI and all is going well. Now I'm creating a new bean that extends a class from a 3rd party library. I attempted to create something like the below example:
@Named("myNewClass")
@ConversationScoped
public class MyNewClass extends ThirdPartyClass {
@Inject
private ApplicationConfig applicationConfig;
@Override
public void doStuff() {
// In this code, applicationConfig will be null.
}
}
When doStuff is called, applicationConfig was always null. I added a no args constructor & a method tagged with @PostConstruct to try and see what was going on. The constructor gets called then the doStuff method. As doStuff is being called at construction time I cannot use the @Inject annotations at this point.
So my question is how do I get a hold of applicationConfig at this point?
I've been tinkering with BeanManager (this is in a function I call with ApplicationConfig.class as a parameter):
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
BeanManager beanManager = (BeanManager) envCtx.lookup("BeanManager");
Bean myBean = beanManager.getBeans(clazz).iterator().next();
return beanManager.getReference(myBean, clazz, beanManager.createCreationalContext(myBean));
Which works but it's creating a new ApplicationConfig instance. I want to get the one that I know already exists on my ConversationScope.
A little info: I'm using Seam 3.0, Weld Servlet 1.1.1 and this is running on Tomcat 6.
Upvotes: 1
Views: 597
Reputation: 5378
You can annotate a constructor with @Inject then any parameters of the constructor become injection points which the BeanManager will resolve. It's certainly not the desired way of doing it, but if it works for you, go for it.
Upvotes: 1