-
1. Re: Confused about the scope of producer methods
haye Jul 21, 2011 2:43 PM (in response to roflchap)
As far as I can tell, the newMember field is not a CDI bean as it's created using the new operator. But as the scope of getNewMember() is @Default, it's expected to have one new Member per injection point. But the getNewMember() returns the same instance variable every time.The scope of getNewMember() determines when the method is called not the lifetime of the returned object (unless it's a dependent bean promoted to some other scope). In your example, you're returning 'newMember' which is a field in a Requestscoped bean. This field and its containing bean will exist for the whole request. So if within one request there are multiple injection points that require an instance of Member, the producer method will be called each time since it's a dependent-scoped producer but the same 'newMember' will be returned. For the next request a new 'newMember' will be returned.
To return a new, dependent, managed Member instance:
@Model public class MemberRegistration { @Produces @Named public Member getNewMember(@New Member newMember) { return newMember; } }
To return a new, managed Member instance per request (the same instance will be used for all injection points):
@Model public class MemberRegistration { @Produces @Model public Member getNewMember(@New Member newMember) { return newMember; } }
The new Member instance injected into the producer is promoted to request scope.
Hope that clears things up.
-
2. Re: Confused about the scope of producer methods
roflchap Jul 21, 2011 9:47 PM (in response to roflchap)Thanks for the excellent explanation! :)