tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alex Soto <asot...@gmail.com>
Subject Re: CDI and Produces
Date Thu, 10 Apr 2014 06:56:33 GMT
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 <asotobu@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
+----------------------------------------------------------+

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message