You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Alex Soto <as...@gmail.com> on 2014/04/09 15:23:44 UTC

CDI and Produces

Hello,

Currently I am developing a demo application I have start playing with
@Produces method and Entity Manager. Let me show the code:

@ApplicationScoped

public class DataSourceProducer {

    @PersistenceContext(unitName = "bank")

    EntityManager em;

    @Produces @BankEntityManager EntityManager entityManager() {

        return this.em;

    }

}


As you can see I have annotated the produced method with a qualifier.


Then I have two Stateless "DAOs":

One that inject entitymanager as:

 @Inject

 @BankEntityManager

 EntityManager em;

and another one that injects as:

@Inject

@MyEnum

 EntityManager em;

Notice that the qualifier is different, but when I run the test the entity
manager is injected inside DAO in both cases. I don't understand why this
happens if @MyEnum annotation is not used as qualifier in any other place
nor in conjunction with @Produces.

Any idea why this happens?

Thank you so much.

Re: CDI and Produces

Posted by Alex Soto <as...@gmail.com>.
Ou yeees true I have not seen it, I simply copy paste from a site blindly
without inspecting the code, and then when I notice that it didn't work I
inspect the log and the produces method trusting that the copy paste was
good.

Thank you so much for your time.


2014-04-11 7:44 GMT+02:00 Romain Manni-Bucau <rm...@gmail.com>:

> Ok, the potential I was thinking about is not one :).
>
> The issue is on your qualifier, missing a little thing making them
> @Qualifier ;)
>
>
> Le 10 avr. 2014 19:48, "Alex Soto" <as...@gmail.com> a écrit :
>
> > Hi,
> > uploaded to https://github.com/lordofthejars/bjugbank you can see the
> > producer at
> >
> >
> https://github.com/lordofthejars/bjugbank/blob/master/src/main/java/com/lordofthejars/bank/util/DataSourceProducer.javaand
> > both different DAOs
> >
> >
> https://github.com/lordofthejars/bjugbank/blob/master/src/main/java/com/lordofthejars/bank/account/control/JpaAccountRepository.javaand
> >
> >
> https://github.com/lordofthejars/bjugbank/blob/master/src/main/java/com/lordofthejars/bank/customer/control/JpaCustomerRepository.javaand
> > of course a test
> >
> >
> https://github.com/lordofthejars/bjugbank/blob/master/src/test/java/com/lordofthejars/bank/account/WhenACustomerTransferMoney.java
> >
> > For example if you put a debug point to the producer you will see that is
> > called twice (one for each DAO).
> >
> > Thank you so much for your help,
> > Alex.
> >
> >
> > 2014-04-10 9:16 GMT+02:00 Alex Soto <as...@gmail.com>:
> >
> > > Yes I will publish it this afternoon and I send you the link so you can
> > > check it. Thank you so much,
> > >
> > >
> > > 2014-04-10 9:11 GMT+02:00 Romain Manni-Bucau <rm...@gmail.com>:
> > >
> > > do you have the setup i can clone to check it? Managed EM are
> > >> something different than normal beans btw (it works for it)
> > >> Romain Manni-Bucau
> > >> Twitter: @rmannibucau
> > >> Blog: http://rmannibucau.wordpress.com/
> > >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> > >> Github: https://github.com/rmannibucau
> > >>
> > >>
> > >>
> > >> 2014-04-10 8:56 GMT+02:00 Alex Soto <as...@gmail.com>:
> > >> > Hi Romain,
> > >> >
> > >> > thank you so much for your quick answer. I am not an expert in CDI
> > but I
> > >> > think that this should be changed, to a more strict mode, let me
> show
> > an
> > >> > example that this could confuse people:
> > >> >
> > >> > Let's suppose we have two classes which each one produces same
> > interface
> > >> > (but different implementation) with different qualifier:
> > >> >
> > >> > class A {
> > >> >
> > >> > @Produces
> > >> > @MyQualifier1
> > >> > public C build() {
> > >> >   ....
> > >> > }
> > >> >
> > >> > and:
> > >> >
> > >> >
> > >> > class B {
> > >> >
> > >> > @Produces
> > >> > @MyQualifier2
> > >> > public C build() {
> > >> > ...
> > >> > }
> > >> >
> > >> > }
> > >> >
> > >> >
> > >> > Than I could write an Arquillian test and miss to add one of those
> > >> classes
> > >> > (the A or the B) then when I run the test I could being inject an
> > >> instance
> > >> > which is not the required one, and having a failing test (or even
> > worse
> > >> a
> > >> > fail pass test) which will be only noticed when the real code was
> > used.
> > >> >
> > >> > So IMO I think that would be better to inject nothing and throw an
> > error
> > >> > but of course I understand that this is a CDI spec problem, not a
> > TomEE
> > >> > problem, so maybe from the point of view of TomEE a warning or
> > something
> > >> > similar could be written.
> > >> >
> > >> > WDYT?
> > >> >
> > >> >
> > >> >
> > >> > 2014-04-09 16:31 GMT+02:00 Romain Manni-Bucau <
> rmannibucau@gmail.com
> > >:
> > >> >
> > >> >> Hmm, IIRC it injects by type if the qualifier doesn't matches.
> > >> >> Romain Manni-Bucau
> > >> >> Twitter: @rmannibucau
> > >> >> Blog: http://rmannibucau.wordpress.com/
> > >> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> > >> >> Github: https://github.com/rmannibucau
> > >> >>
> > >> >>
> > >> >>
> > >> >> 2014-04-09 15:23 GMT+02:00 Alex Soto <as...@gmail.com>:
> > >> >> > Hello,
> > >> >> >
> > >> >> > Currently I am developing a demo application I have start playing
> > >> with
> > >> >> > @Produces method and Entity Manager. Let me show the code:
> > >> >> >
> > >> >> > @ApplicationScoped
> > >> >> >
> > >> >> > public class DataSourceProducer {
> > >> >> >
> > >> >> >     @PersistenceContext(unitName = "bank")
> > >> >> >
> > >> >> >     EntityManager em;
> > >> >> >
> > >> >> >     @Produces @BankEntityManager EntityManager entityManager() {
> > >> >> >
> > >> >> >         return this.em;
> > >> >> >
> > >> >> >     }
> > >> >> >
> > >> >> > }
> > >> >> >
> > >> >> >
> > >> >> > As you can see I have annotated the produced method with a
> > qualifier.
> > >> >> >
> > >> >> >
> > >> >> > Then I have two Stateless "DAOs":
> > >> >> >
> > >> >> > One that inject entitymanager as:
> > >> >> >
> > >> >> >  @Inject
> > >> >> >
> > >> >> >  @BankEntityManager
> > >> >> >
> > >> >> >  EntityManager em;
> > >> >> >
> > >> >> > and another one that injects as:
> > >> >> >
> > >> >> > @Inject
> > >> >> >
> > >> >> > @MyEnum
> > >> >> >
> > >> >> >  EntityManager em;
> > >> >> >
> > >> >> > Notice that the qualifier is different, but when I run the test
> the
> > >> >> entity
> > >> >> > manager is injected inside DAO in both cases. I don't understand
> > why
> > >> this
> > >> >> > happens if @MyEnum annotation is not used as qualifier in any
> other
> > >> place
> > >> >> > nor in conjunction with @Produces.
> > >> >> >
> > >> >> > Any idea why this happens?
> > >> >> >
> > >> >> > Thank you so much.
> > >> >>
> > >> >
> > >> >
> > >> >
> > >> > --
> > >> > +----------------------------------------------------------+
> > >> >   Alex Soto Bueno - Computer Engineer
> > >> >   www.lordofthejars.com
> > >> > +----------------------------------------------------------+
> > >>
> > >
> > >
> > >
> > > --
> > > +----------------------------------------------------------+
> > >   Alex Soto Bueno - Computer Engineer
> > >   www.lordofthejars.com
> > > +----------------------------------------------------------+
> > >
> >
> >
> >
> > --
> > +----------------------------------------------------------+
> >   Alex Soto Bueno - Computer Engineer
> >   www.lordofthejars.com
> > +----------------------------------------------------------+
> >
>



-- 
+----------------------------------------------------------+
  Alex Soto Bueno - Computer Engineer
  www.lordofthejars.com
+----------------------------------------------------------+

Re: CDI and Produces

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Ok, the potential I was thinking about is not one :).

The issue is on your qualifier, missing a little thing making them
@Qualifier ;)


Le 10 avr. 2014 19:48, "Alex Soto" <as...@gmail.com> a écrit :

> Hi,
> uploaded to https://github.com/lordofthejars/bjugbank you can see the
> producer at
>
> https://github.com/lordofthejars/bjugbank/blob/master/src/main/java/com/lordofthejars/bank/util/DataSourceProducer.javaand
> both different DAOs
>
> https://github.com/lordofthejars/bjugbank/blob/master/src/main/java/com/lordofthejars/bank/account/control/JpaAccountRepository.javaand
>
> https://github.com/lordofthejars/bjugbank/blob/master/src/main/java/com/lordofthejars/bank/customer/control/JpaCustomerRepository.javaand
> of course a test
>
> https://github.com/lordofthejars/bjugbank/blob/master/src/test/java/com/lordofthejars/bank/account/WhenACustomerTransferMoney.java
>
> For example if you put a debug point to the producer you will see that is
> called twice (one for each DAO).
>
> Thank you so much for your help,
> Alex.
>
>
> 2014-04-10 9:16 GMT+02:00 Alex Soto <as...@gmail.com>:
>
> > Yes I will publish it this afternoon and I send you the link so you can
> > check it. Thank you so much,
> >
> >
> > 2014-04-10 9:11 GMT+02:00 Romain Manni-Bucau <rm...@gmail.com>:
> >
> > do you have the setup i can clone to check it? Managed EM are
> >> something different than normal beans btw (it works for it)
> >> Romain Manni-Bucau
> >> Twitter: @rmannibucau
> >> Blog: http://rmannibucau.wordpress.com/
> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> >> Github: https://github.com/rmannibucau
> >>
> >>
> >>
> >> 2014-04-10 8:56 GMT+02:00 Alex Soto <as...@gmail.com>:
> >> > Hi Romain,
> >> >
> >> > thank you so much for your quick answer. I am not an expert in CDI
> but I
> >> > think that this should be changed, to a more strict mode, let me show
> an
> >> > example that this could confuse people:
> >> >
> >> > Let's suppose we have two classes which each one produces same
> interface
> >> > (but different implementation) with different qualifier:
> >> >
> >> > class A {
> >> >
> >> > @Produces
> >> > @MyQualifier1
> >> > public C build() {
> >> >   ....
> >> > }
> >> >
> >> > and:
> >> >
> >> >
> >> > class B {
> >> >
> >> > @Produces
> >> > @MyQualifier2
> >> > public C build() {
> >> > ...
> >> > }
> >> >
> >> > }
> >> >
> >> >
> >> > Than I could write an Arquillian test and miss to add one of those
> >> classes
> >> > (the A or the B) then when I run the test I could being inject an
> >> instance
> >> > which is not the required one, and having a failing test (or even
> worse
> >> a
> >> > fail pass test) which will be only noticed when the real code was
> used.
> >> >
> >> > So IMO I think that would be better to inject nothing and throw an
> error
> >> > but of course I understand that this is a CDI spec problem, not a
> TomEE
> >> > problem, so maybe from the point of view of TomEE a warning or
> something
> >> > similar could be written.
> >> >
> >> > WDYT?
> >> >
> >> >
> >> >
> >> > 2014-04-09 16:31 GMT+02:00 Romain Manni-Bucau <rmannibucau@gmail.com
> >:
> >> >
> >> >> Hmm, IIRC it injects by type if the qualifier doesn't matches.
> >> >> Romain Manni-Bucau
> >> >> Twitter: @rmannibucau
> >> >> Blog: http://rmannibucau.wordpress.com/
> >> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> >> >> Github: https://github.com/rmannibucau
> >> >>
> >> >>
> >> >>
> >> >> 2014-04-09 15:23 GMT+02:00 Alex Soto <as...@gmail.com>:
> >> >> > Hello,
> >> >> >
> >> >> > Currently I am developing a demo application I have start playing
> >> with
> >> >> > @Produces method and Entity Manager. Let me show the code:
> >> >> >
> >> >> > @ApplicationScoped
> >> >> >
> >> >> > public class DataSourceProducer {
> >> >> >
> >> >> >     @PersistenceContext(unitName = "bank")
> >> >> >
> >> >> >     EntityManager em;
> >> >> >
> >> >> >     @Produces @BankEntityManager EntityManager entityManager() {
> >> >> >
> >> >> >         return this.em;
> >> >> >
> >> >> >     }
> >> >> >
> >> >> > }
> >> >> >
> >> >> >
> >> >> > As you can see I have annotated the produced method with a
> qualifier.
> >> >> >
> >> >> >
> >> >> > Then I have two Stateless "DAOs":
> >> >> >
> >> >> > One that inject entitymanager as:
> >> >> >
> >> >> >  @Inject
> >> >> >
> >> >> >  @BankEntityManager
> >> >> >
> >> >> >  EntityManager em;
> >> >> >
> >> >> > and another one that injects as:
> >> >> >
> >> >> > @Inject
> >> >> >
> >> >> > @MyEnum
> >> >> >
> >> >> >  EntityManager em;
> >> >> >
> >> >> > Notice that the qualifier is different, but when I run the test the
> >> >> entity
> >> >> > manager is injected inside DAO in both cases. I don't understand
> why
> >> this
> >> >> > happens if @MyEnum annotation is not used as qualifier in any other
> >> place
> >> >> > nor in conjunction with @Produces.
> >> >> >
> >> >> > Any idea why this happens?
> >> >> >
> >> >> > Thank you so much.
> >> >>
> >> >
> >> >
> >> >
> >> > --
> >> > +----------------------------------------------------------+
> >> >   Alex Soto Bueno - Computer Engineer
> >> >   www.lordofthejars.com
> >> > +----------------------------------------------------------+
> >>
> >
> >
> >
> > --
> > +----------------------------------------------------------+
> >   Alex Soto Bueno - Computer Engineer
> >   www.lordofthejars.com
> > +----------------------------------------------------------+
> >
>
>
>
> --
> +----------------------------------------------------------+
>   Alex Soto Bueno - Computer Engineer
>   www.lordofthejars.com
> +----------------------------------------------------------+
>

Re: CDI and Produces

Posted by Alex Soto <as...@gmail.com>.
Hi,
uploaded to https://github.com/lordofthejars/bjugbank you can see the
producer at
https://github.com/lordofthejars/bjugbank/blob/master/src/main/java/com/lordofthejars/bank/util/DataSourceProducer.javaand
both different DAOs
https://github.com/lordofthejars/bjugbank/blob/master/src/main/java/com/lordofthejars/bank/account/control/JpaAccountRepository.javaand
https://github.com/lordofthejars/bjugbank/blob/master/src/main/java/com/lordofthejars/bank/customer/control/JpaCustomerRepository.javaand
of course a test
https://github.com/lordofthejars/bjugbank/blob/master/src/test/java/com/lordofthejars/bank/account/WhenACustomerTransferMoney.java

For example if you put a debug point to the producer you will see that is
called twice (one for each DAO).

Thank you so much for your help,
Alex.


2014-04-10 9:16 GMT+02:00 Alex Soto <as...@gmail.com>:

> Yes I will publish it this afternoon and I send you the link so you can
> check it. Thank you so much,
>
>
> 2014-04-10 9:11 GMT+02:00 Romain Manni-Bucau <rm...@gmail.com>:
>
> do you have the setup i can clone to check it? Managed EM are
>> something different than normal beans btw (it works for it)
>> Romain Manni-Bucau
>> Twitter: @rmannibucau
>> Blog: http://rmannibucau.wordpress.com/
>> LinkedIn: http://fr.linkedin.com/in/rmannibucau
>> Github: https://github.com/rmannibucau
>>
>>
>>
>> 2014-04-10 8:56 GMT+02:00 Alex Soto <as...@gmail.com>:
>> > Hi Romain,
>> >
>> > thank you so much for your quick answer. I am not an expert in CDI but I
>> > think that this should be changed, to a more strict mode, let me show an
>> > example that this could confuse people:
>> >
>> > Let's suppose we have two classes which each one produces same interface
>> > (but different implementation) with different qualifier:
>> >
>> > class A {
>> >
>> > @Produces
>> > @MyQualifier1
>> > public C build() {
>> >   ....
>> > }
>> >
>> > and:
>> >
>> >
>> > class B {
>> >
>> > @Produces
>> > @MyQualifier2
>> > public C build() {
>> > ...
>> > }
>> >
>> > }
>> >
>> >
>> > Than I could write an Arquillian test and miss to add one of those
>> classes
>> > (the A or the B) then when I run the test I could being inject an
>> instance
>> > which is not the required one, and having a failing test (or even worse
>> a
>> > fail pass test) which will be only noticed when the real code was used.
>> >
>> > So IMO I think that would be better to inject nothing and throw an error
>> > but of course I understand that this is a CDI spec problem, not a TomEE
>> > problem, so maybe from the point of view of TomEE a warning or something
>> > similar could be written.
>> >
>> > WDYT?
>> >
>> >
>> >
>> > 2014-04-09 16:31 GMT+02:00 Romain Manni-Bucau <rm...@gmail.com>:
>> >
>> >> Hmm, IIRC it injects by type if the qualifier doesn't matches.
>> >> Romain Manni-Bucau
>> >> Twitter: @rmannibucau
>> >> Blog: http://rmannibucau.wordpress.com/
>> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
>> >> Github: https://github.com/rmannibucau
>> >>
>> >>
>> >>
>> >> 2014-04-09 15:23 GMT+02:00 Alex Soto <as...@gmail.com>:
>> >> > Hello,
>> >> >
>> >> > Currently I am developing a demo application I have start playing
>> with
>> >> > @Produces method and Entity Manager. Let me show the code:
>> >> >
>> >> > @ApplicationScoped
>> >> >
>> >> > public class DataSourceProducer {
>> >> >
>> >> >     @PersistenceContext(unitName = "bank")
>> >> >
>> >> >     EntityManager em;
>> >> >
>> >> >     @Produces @BankEntityManager EntityManager entityManager() {
>> >> >
>> >> >         return this.em;
>> >> >
>> >> >     }
>> >> >
>> >> > }
>> >> >
>> >> >
>> >> > As you can see I have annotated the produced method with a qualifier.
>> >> >
>> >> >
>> >> > Then I have two Stateless "DAOs":
>> >> >
>> >> > One that inject entitymanager as:
>> >> >
>> >> >  @Inject
>> >> >
>> >> >  @BankEntityManager
>> >> >
>> >> >  EntityManager em;
>> >> >
>> >> > and another one that injects as:
>> >> >
>> >> > @Inject
>> >> >
>> >> > @MyEnum
>> >> >
>> >> >  EntityManager em;
>> >> >
>> >> > Notice that the qualifier is different, but when I run the test the
>> >> entity
>> >> > manager is injected inside DAO in both cases. I don't understand why
>> this
>> >> > happens if @MyEnum annotation is not used as qualifier in any other
>> place
>> >> > nor in conjunction with @Produces.
>> >> >
>> >> > Any idea why this happens?
>> >> >
>> >> > Thank you so much.
>> >>
>> >
>> >
>> >
>> > --
>> > +----------------------------------------------------------+
>> >   Alex Soto Bueno - Computer Engineer
>> >   www.lordofthejars.com
>> > +----------------------------------------------------------+
>>
>
>
>
> --
> +----------------------------------------------------------+
>   Alex Soto Bueno - Computer Engineer
>   www.lordofthejars.com
> +----------------------------------------------------------+
>



-- 
+----------------------------------------------------------+
  Alex Soto Bueno - Computer Engineer
  www.lordofthejars.com
+----------------------------------------------------------+

Re: CDI and Produces

Posted by Alex Soto <as...@gmail.com>.
Yes I will publish it this afternoon and I send you the link so you can
check it. Thank you so much,


2014-04-10 9:11 GMT+02:00 Romain Manni-Bucau <rm...@gmail.com>:

> do you have the setup i can clone to check it? Managed EM are
> something different than normal beans btw (it works for it)
> Romain Manni-Bucau
> Twitter: @rmannibucau
> Blog: http://rmannibucau.wordpress.com/
> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> Github: https://github.com/rmannibucau
>
>
>
> 2014-04-10 8:56 GMT+02:00 Alex Soto <as...@gmail.com>:
> > Hi Romain,
> >
> > thank you so much for your quick answer. I am not an expert in CDI but I
> > think that this should be changed, to a more strict mode, let me show an
> > example that this could confuse people:
> >
> > Let's suppose we have two classes which each one produces same interface
> > (but different implementation) with different qualifier:
> >
> > class A {
> >
> > @Produces
> > @MyQualifier1
> > public C build() {
> >   ....
> > }
> >
> > and:
> >
> >
> > class B {
> >
> > @Produces
> > @MyQualifier2
> > public C build() {
> > ...
> > }
> >
> > }
> >
> >
> > Than I could write an Arquillian test and miss to add one of those
> classes
> > (the A or the B) then when I run the test I could being inject an
> instance
> > which is not the required one, and having a failing test (or even worse a
> > fail pass test) which will be only noticed when the real code was used.
> >
> > So IMO I think that would be better to inject nothing and throw an error
> > but of course I understand that this is a CDI spec problem, not a TomEE
> > problem, so maybe from the point of view of TomEE a warning or something
> > similar could be written.
> >
> > WDYT?
> >
> >
> >
> > 2014-04-09 16:31 GMT+02:00 Romain Manni-Bucau <rm...@gmail.com>:
> >
> >> Hmm, IIRC it injects by type if the qualifier doesn't matches.
> >> Romain Manni-Bucau
> >> Twitter: @rmannibucau
> >> Blog: http://rmannibucau.wordpress.com/
> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> >> Github: https://github.com/rmannibucau
> >>
> >>
> >>
> >> 2014-04-09 15:23 GMT+02:00 Alex Soto <as...@gmail.com>:
> >> > Hello,
> >> >
> >> > Currently I am developing a demo application I have start playing with
> >> > @Produces method and Entity Manager. Let me show the code:
> >> >
> >> > @ApplicationScoped
> >> >
> >> > public class DataSourceProducer {
> >> >
> >> >     @PersistenceContext(unitName = "bank")
> >> >
> >> >     EntityManager em;
> >> >
> >> >     @Produces @BankEntityManager EntityManager entityManager() {
> >> >
> >> >         return this.em;
> >> >
> >> >     }
> >> >
> >> > }
> >> >
> >> >
> >> > As you can see I have annotated the produced method with a qualifier.
> >> >
> >> >
> >> > Then I have two Stateless "DAOs":
> >> >
> >> > One that inject entitymanager as:
> >> >
> >> >  @Inject
> >> >
> >> >  @BankEntityManager
> >> >
> >> >  EntityManager em;
> >> >
> >> > and another one that injects as:
> >> >
> >> > @Inject
> >> >
> >> > @MyEnum
> >> >
> >> >  EntityManager em;
> >> >
> >> > Notice that the qualifier is different, but when I run the test the
> >> entity
> >> > manager is injected inside DAO in both cases. I don't understand why
> this
> >> > happens if @MyEnum annotation is not used as qualifier in any other
> place
> >> > nor in conjunction with @Produces.
> >> >
> >> > Any idea why this happens?
> >> >
> >> > Thank you so much.
> >>
> >
> >
> >
> > --
> > +----------------------------------------------------------+
> >   Alex Soto Bueno - Computer Engineer
> >   www.lordofthejars.com
> > +----------------------------------------------------------+
>



-- 
+----------------------------------------------------------+
  Alex Soto Bueno - Computer Engineer
  www.lordofthejars.com
+----------------------------------------------------------+

Re: CDI and Produces

Posted by Romain Manni-Bucau <rm...@gmail.com>.
do you have the setup i can clone to check it? Managed EM are
something different than normal beans btw (it works for it)
Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2014-04-10 8:56 GMT+02:00 Alex Soto <as...@gmail.com>:
> Hi Romain,
>
> thank you so much for your quick answer. I am not an expert in CDI but I
> think that this should be changed, to a more strict mode, let me show an
> example that this could confuse people:
>
> Let's suppose we have two classes which each one produces same interface
> (but different implementation) with different qualifier:
>
> class A {
>
> @Produces
> @MyQualifier1
> public C build() {
>   ....
> }
>
> and:
>
>
> class B {
>
> @Produces
> @MyQualifier2
> public C build() {
> ...
> }
>
> }
>
>
> Than I could write an Arquillian test and miss to add one of those classes
> (the A or the B) then when I run the test I could being inject an instance
> which is not the required one, and having a failing test (or even worse a
> fail pass test) which will be only noticed when the real code was used.
>
> So IMO I think that would be better to inject nothing and throw an error
> but of course I understand that this is a CDI spec problem, not a TomEE
> problem, so maybe from the point of view of TomEE a warning or something
> similar could be written.
>
> WDYT?
>
>
>
> 2014-04-09 16:31 GMT+02:00 Romain Manni-Bucau <rm...@gmail.com>:
>
>> Hmm, IIRC it injects by type if the qualifier doesn't matches.
>> Romain Manni-Bucau
>> Twitter: @rmannibucau
>> Blog: http://rmannibucau.wordpress.com/
>> LinkedIn: http://fr.linkedin.com/in/rmannibucau
>> Github: https://github.com/rmannibucau
>>
>>
>>
>> 2014-04-09 15:23 GMT+02:00 Alex Soto <as...@gmail.com>:
>> > Hello,
>> >
>> > Currently I am developing a demo application I have start playing with
>> > @Produces method and Entity Manager. Let me show the code:
>> >
>> > @ApplicationScoped
>> >
>> > public class DataSourceProducer {
>> >
>> >     @PersistenceContext(unitName = "bank")
>> >
>> >     EntityManager em;
>> >
>> >     @Produces @BankEntityManager EntityManager entityManager() {
>> >
>> >         return this.em;
>> >
>> >     }
>> >
>> > }
>> >
>> >
>> > As you can see I have annotated the produced method with a qualifier.
>> >
>> >
>> > Then I have two Stateless "DAOs":
>> >
>> > One that inject entitymanager as:
>> >
>> >  @Inject
>> >
>> >  @BankEntityManager
>> >
>> >  EntityManager em;
>> >
>> > and another one that injects as:
>> >
>> > @Inject
>> >
>> > @MyEnum
>> >
>> >  EntityManager em;
>> >
>> > Notice that the qualifier is different, but when I run the test the
>> entity
>> > manager is injected inside DAO in both cases. I don't understand why this
>> > happens if @MyEnum annotation is not used as qualifier in any other place
>> > nor in conjunction with @Produces.
>> >
>> > Any idea why this happens?
>> >
>> > Thank you so much.
>>
>
>
>
> --
> +----------------------------------------------------------+
>   Alex Soto Bueno - Computer Engineer
>   www.lordofthejars.com
> +----------------------------------------------------------+

Re: CDI and Produces

Posted by Alex Soto <as...@gmail.com>.
Hi Romain,

thank you so much for your quick answer. I am not an expert in CDI but I
think that this should be changed, to a more strict mode, let me show an
example that this could confuse people:

Let's suppose we have two classes which each one produces same interface
(but different implementation) with different qualifier:

class A {

@Produces
@MyQualifier1
public C build() {
  ....
}

and:


class B {

@Produces
@MyQualifier2
public C build() {
...
}

}


Than I could write an Arquillian test and miss to add one of those classes
(the A or the B) then when I run the test I could being inject an instance
which is not the required one, and having a failing test (or even worse a
fail pass test) which will be only noticed when the real code was used.

So IMO I think that would be better to inject nothing and throw an error
but of course I understand that this is a CDI spec problem, not a TomEE
problem, so maybe from the point of view of TomEE a warning or something
similar could be written.

WDYT?



2014-04-09 16:31 GMT+02:00 Romain Manni-Bucau <rm...@gmail.com>:

> Hmm, IIRC it injects by type if the qualifier doesn't matches.
> Romain Manni-Bucau
> Twitter: @rmannibucau
> Blog: http://rmannibucau.wordpress.com/
> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> Github: https://github.com/rmannibucau
>
>
>
> 2014-04-09 15:23 GMT+02:00 Alex Soto <as...@gmail.com>:
> > Hello,
> >
> > Currently I am developing a demo application I have start playing with
> > @Produces method and Entity Manager. Let me show the code:
> >
> > @ApplicationScoped
> >
> > public class DataSourceProducer {
> >
> >     @PersistenceContext(unitName = "bank")
> >
> >     EntityManager em;
> >
> >     @Produces @BankEntityManager EntityManager entityManager() {
> >
> >         return this.em;
> >
> >     }
> >
> > }
> >
> >
> > As you can see I have annotated the produced method with a qualifier.
> >
> >
> > Then I have two Stateless "DAOs":
> >
> > One that inject entitymanager as:
> >
> >  @Inject
> >
> >  @BankEntityManager
> >
> >  EntityManager em;
> >
> > and another one that injects as:
> >
> > @Inject
> >
> > @MyEnum
> >
> >  EntityManager em;
> >
> > Notice that the qualifier is different, but when I run the test the
> entity
> > manager is injected inside DAO in both cases. I don't understand why this
> > happens if @MyEnum annotation is not used as qualifier in any other place
> > nor in conjunction with @Produces.
> >
> > Any idea why this happens?
> >
> > Thank you so much.
>



-- 
+----------------------------------------------------------+
  Alex Soto Bueno - Computer Engineer
  www.lordofthejars.com
+----------------------------------------------------------+

Re: CDI and Produces

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hmm, IIRC it injects by type if the qualifier doesn't matches.
Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2014-04-09 15:23 GMT+02:00 Alex Soto <as...@gmail.com>:
> Hello,
>
> Currently I am developing a demo application I have start playing with
> @Produces method and Entity Manager. Let me show the code:
>
> @ApplicationScoped
>
> public class DataSourceProducer {
>
>     @PersistenceContext(unitName = "bank")
>
>     EntityManager em;
>
>     @Produces @BankEntityManager EntityManager entityManager() {
>
>         return this.em;
>
>     }
>
> }
>
>
> As you can see I have annotated the produced method with a qualifier.
>
>
> Then I have two Stateless "DAOs":
>
> One that inject entitymanager as:
>
>  @Inject
>
>  @BankEntityManager
>
>  EntityManager em;
>
> and another one that injects as:
>
> @Inject
>
> @MyEnum
>
>  EntityManager em;
>
> Notice that the qualifier is different, but when I run the test the entity
> manager is injected inside DAO in both cases. I don't understand why this
> happens if @MyEnum annotation is not used as qualifier in any other place
> nor in conjunction with @Produces.
>
> Any idea why this happens?
>
> Thank you so much.