You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by cfnz <re...@hill-labs.co.nz> on 2015/09/16 06:54:29 UTC

Two beans with the same name - I thought was supposed to give error?

Still trying to come to grips with beans and CDI, etc...

I was under the impression that if two beans had the same name, that the
container would complain as it would not know which one to use... but it
doesn't... is it supposed to?

Test case... take the eclipse link movies example, copy Movies.java to
Movies2.java and add the following:

@Stateful(name = "Movies")
public class Movies2 {
... etc
}

I thought this would complain... When the test case asks for Movies, how
does it know which movies it will get?? I have been looking into the
@Alternative annotation, but the first thing I need to sort out is why the
above works... (I have another query on how to select the @Alternative, but
I shall do another post for that.)

Regards
Colin




--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Two-beans-with-the-same-name-I-thought-was-supposed-to-give-error-tp4676235.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Two beans with the same name - I thought was supposed to give error?

Posted by cfnz <re...@hill-labs.co.nz>.
Thanks for that... Starting to get (some of) it... :-)

So CDI and EJB are similar but different (at least configured quite
differently)...

And CDI would complain about duplicates (or alternatives), and that is where
you use the beans.xml file, whereas for EJBs, you have to watch out for
duplicates names yourself, and the container will load whichever name it
comes across first.

EJB has nothing to do with the beans.xml file (it is called beans.xml just
to confuse you :-)... they may be beans (or POJOs) but not EJ beans...), for
EJB, you can switch in and out alternatives by binding what class a name in
an EJB annotation refers to in the ejb-jar.xml file.

That is my current understanding... also found the following post which is
helpful:  http://blog.dblevins.com/2012/11/cdi-when-to-break-out-ejbs.html
<http://blog.dblevins.com/2012/11/cdi-when-to-break-out-ejbs.html>  

Thanks for your explanation... it helped me along the CDI compared to EJB
route...

Regards
Colin



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Two-beans-with-the-same-name-I-thought-was-supposed-to-give-error-tp4676235p4676276.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Two beans with the same name - I thought was supposed to give error?

Posted by Gurkan Erdogdu <ge...@managecat.com>.
Hello Colin

This is not related with CDI, you are using @Stateful annotation defined in EJB specification. EJB names are used to give a unique name to EJB beans, therefore you can inject them via @EJB(name of the bean). In OpenEJB, you can use the same name here but name will be used for EJB bean first deployed. If Movies2 will be deployed first, only this bean is deployed otherwise Movies will be deployed and created JNDI name for them. According to EJB specification, The ejb-name element contains the name of an enterprise
bean. The name must be unique within the ejb-jar file.	Therefore, you have to give unique names to EJB beans.

So, if you want to use Alternative with CDI specification, you can use @Inject to inject EJB Beans and use @Alternatives to describe alternative CDI beans and also define alternatives in beans.xml.

Please have a look at this example : https://github.com/apache/tomee/tree/master/examples/cdi-alternative-and-stereotypes <https://github.com/apache/tomee/tree/master/examples/cdi-alternative-and-stereotypes> 

Hope that it helps!

Best
Gurkan Erdogdu
ManageCat, http://www.managecat.com <http://www.managecat.com/> 
Office : +1 (909) - 366 – 9337
340 S Lemon Ave #7996 Walnut, CA 91789

One of them is : 

> On 16 Sep 2015, at 07:54, cfnz <re...@hill-labs.co.nz> wrote:
> 
> Still trying to come to grips with beans and CDI, etc...
> 
> I was under the impression that if two beans had the same name, that the
> container would complain as it would not know which one to use... but it
> doesn't... is it supposed to?
> 
> Test case... take the eclipse link movies example, copy Movies.java to
> Movies2.java and add the following:
> 
> @Stateful(name = "Movies")
> public class Movies2 {
> ... etc
> }
> 
> I thought this would complain... When the test case asks for Movies, how
> does it know which movies it will get?? I have been looking into the
> @Alternative annotation, but the first thing I need to sort out is why the
> above works... (I have another query on how to select the @Alternative, but
> I shall do another post for that.)
> 
> Regards
> Colin
> 
> 
> 
> 
> --
> View this message in context: http://tomee-openejb.979440.n4.nabble.com/Two-beans-with-the-same-name-I-thought-was-supposed-to-give-error-tp4676235.html
> Sent from the TomEE Users mailing list archive at Nabble.com.