[cdi-dev] @New and producer methods/fields

Pete Muir pmuir at redhat.com
Sun Sep 25 16:11:46 EDT 2011


On 25 Sep 2011, at 21:10, Mark Struberg wrote:

> As explained below: in every case where you need a producer method to create a useful bean, then @New just doesn't work. 
> 
> 
> My usecase was that I would have needed one of those EntityManagers for unit tests. And since unit tests are no CDI managed beans thus cannot use @Transactional, I wanted to just create a manually managed contextual instance.

It wasn't intended for this use case, but we could consider if this is possible.

File an issue, I have a bad feeling there is a gotcha in here somewhere, but not sure atm.

> 
> @New currently is basically only useful for creating new instances of a class (which must not even be a managed bean but vetoed...).

This is the use case for which it is intended.

> 
> 
> LieGrue,
> strub
> 
> 
> 
> ----- Original Message -----
>> From: Pete Muir <pmuir at redhat.com>
>> To: Mark Struberg <struberg at yahoo.de>
>> Cc: "cdi-dev at lists.jboss.org" <cdi-dev at lists.jboss.org>
>> Sent: Sunday, September 25, 2011 9:56 PM
>> Subject: Re: [cdi-dev] @New and producer methods/fields
>> 
>> Section 3.12 states "For each managed bean, and for each session bean, a 
>> second bean exists which:"
>> 
>> So, there are no @New beans for producers.
>> 
>> What is the use case you are trying to address?
>> 
>> On 24 Sep 2011, at 11:35, Mark Struberg wrote:
>> 
>>> Hi folks!
>>> 
>>> We recently talked about ways to properly destroy beans which got created 
>> via Instance or @New.
>>> I just realized that only having 
>>> @Inject @New MyClass dings; 
>>> might not be enough. 
>>> 
>>> Imagine you have 2 producer methods which create EntityManagers 
>>> 
>>> public class EntityManagerProducer {  
>>>      @Produces @RequestScoped @UserDb
>>>      public void createUserDbEm() {
>>>          return 
>> entityManagerFactory.createEntityManager("userdb");
>>>      }    
>>>     
>>>      @Produces @RequestScoped @AdminDb
>>>      public void createAdminDbEm() {
>>>          return 
>> entityManagerFactory.createEntityManager("admindb");
>>>      }    
>>> }
>>> 
>>> If I need a 'temporarily self managed' userdb EntityManager, I 
>> cannot just
>>> type 
>>> @Inject @New @UserDb EntityManager userDbEm;
>>> because according to the spec there is only 1 Bean with exactly @New (and 
>> none with additional @UserDb)
>>> The @New is basically useless for producer methods, isn't?
>>> 
>>> Do we like to address this somehow?
>>> 
>>> LieGrie,
>>> strub
>>> 
>>> 
>>> _______________________________________________
>>> cdi-dev mailing list
>>> cdi-dev at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/cdi-dev
>> 




More information about the cdi-dev mailing list