-
1. Re: A question about the sample code generated by Weld's JEE6 archetype
spinner.joserodolfo.freitas.gawab.com Jul 20, 2011 2:59 PM (in response to roflchap)Hi Ro chan,
there're two getNewMember() methods.
one
with @Named accessible to JSF through EL@Produces @Named public Member getNewMember() { return newMember; }
and one that returns a RequestScoped new Member, which is not accessible through EL.
@Produces @RequestScoped public Member getNewMember() { return new Member(); }
was the second method coded by you? try giving a different name for it.
-
2. Re: A question about the sample code generated by Weld's JEE6 archetype
spinner.joserodolfo.freitas.gawab.com Jul 20, 2011 3:25 PM (in response to roflchap)Hi Ro chan,
sorry, I didn't understand what you're asking.
do you want to know why the method
@Produces @Named public Member getNewMember() { return newMember; }
won't return a newMember requestScoped being a member of a RequestScope instance (@Model MemberRegistration), right?
if that's what you're asking.
there's a complicated answer, and I hope I can reach you with my explanation:
CDI don't work directly with the object instances, it creates proxies vinculated to a context to access the real objects.
the method that exposes getNewMember produces a @Dependent member, not a proxy to a requestScoped bean.
And Dependent scoped beans can be injected directly, as the dependent beans lifecycle is tied to the bean it is injected into.so this could be the reason you're not getting the requestScoped member.
if you want to understand more about cdi proxies take a look at:
http://community.jboss.org/blogs/stuartdouglas/2010/10/12/weld-cdi-and-proxies
http://techblog.joserodolfo.com/2011/04/the-proxied-nature-of-an-injected-bean-in-cdi/
http://seamframework.org/Community/QuestionRegardingCDIsProxyClassGeneration
-
3. Re: A question about the sample code generated by Weld's JEE6 archetype
roflchap Jul 21, 2011 4:01 AM (in response to roflchap)Hi Jose,
Thanks for the reply. I think I hadn't phrased out my question clearly. Let me rephrase my question in a better form:
- Is it possible to re-implement the getNewMember() method in a way that does not depend on a member variable (i.e. without depending on private Member newMember;)?
- I know the different between @Default-scoped and @RequestScoped beans. But I don't know why it was necessary to use a @Default-scoped bean for this application? Even that I know how @Default-scoped beans work (their lifecycle is determined by the lifecycle of the bean they are injected to), I don't know why in this particular case it was necessary to use an @Default-scoped bean.
Thanks in advance.
-
4. Re: A question about the sample code generated by Weld's JEE6 archetype
roflchap Jul 21, 2011 5:01 AM (in response to roflchap)There's a typo in my last reply. @Default should be changed to @Dependent.
-
5. Re: A question about the sample code generated by Weld's JEE6 archetype
spinner.joserodolfo.freitas.gawab.com Jul 21, 2011 7:43 AM (in response to roflchap)Hi Ro chan,
Now I get your question. let me try to answer you as clear as you asked.
First I'd like to say tht I don't remember details of this sample code, but I'm guessing that Member is a JPA Entity.
Being a JPA entity we could say that it's not a good practice to let cdi handle it's lifecycle, cause its alredy handled by jpa.
The way that a newMember was exposed to the view it's a perfect way of exposing the entity bean, because you're exposing a reference to a object that your model already has a reference too, and it's the real bean, not a proxy.
If newMember was injected in your model class, through a requestscoped producer, when trying to em.persist(newMember) (in register method), it would not work. That's because when you try to persist a bean that was handled by cdi it'll try to persist a proxy not the real bean.
got it?
please see the link for more the details:
The proxied nature of an injected bean in CDI and the CDI/JPA ecosystem -
6. Re: A question about the sample code generated by Weld's JEE6 archetype
spinner.joserodolfo.freitas.gawab.com Jul 21, 2011 7:47 AM (in response to roflchap)So
it's possible to re-implement it (java would let you) but it'd not work as you expect.
-
7. Re: A question about the sample code generated by Weld's JEE6 archetype
roflchap Jul 21, 2011 9:50 PM (in response to roflchap)Hi Jose,
Thanks for the explanation and the links! Now my confusion is solved! :)