users@glassfish.java.net

Re: _at_EJB annotation does not bind EJB under java:comp/env

From: Cheng Fang <Cheng.Fang_at_Sun.COM>
Date: Wed, 17 Oct 2007 09:02:29 -0400

If you want to assign a custom global JNDI name to your EJB, you can use:

@Stateless(mappedName="ejb/Foo")
public class EchoProviderBean implements EchoProviderLocal {

See its javadoc:
http://java.sun.com/javaee/5/docs/api/javax/ejb/Stateless.html

Note that mappedName is not portable (see the above javadoc for more
details). It's supported in glassfish but may not work in other appserver.

Oftentimes you don't need to worry about an EJB's global JNDI name.
Glassfish assigns default global JNDI name for EJBs according to the
following rules:

https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#SessionBeanGlobalJNDINameAssignment

If in your sample app, there is only 1 EJB implementing
EchoProviderLocal interface, you can just do the following to get the
bean reference (no xml descriptor, no JNDI lookup):

//inside servlet class
@EJB private EchoProviderLocal echo;

-cheng

Vano Beridze wrote:
> Cheng Fang wrote:
>> @EJB injects or declares a ejb-ref to the target component (e.g.,
>> servelt) that uses the EJB. From your code, it seems you are using
>> @EJB on the subject bean, which is not what you want.
> Is it possible to register my bean in ENC context using some other
> annotation?
> Or I should use xml descriptor.
>>
>> -cheng
>>
>> Vano Beridze wrote:
>>> Hello
>>>
>>> I user NetBeans 6 latest daily build, glassfish v2 b58g.
>>> I've created Enterprise Application Project in NetBeans with EJB
>>> Module project.
>>> I've created one stateless session bean in EJB Module Project with
>>> one simple method.
>>> I used @EJB annotation to register my EJB in ENC.
>>>
>>> package test;
>>>
>>> import javax.ejb.EJB;
>>> import javax.ejb.Stateless;
>>>
>>> @Stateless
>>> @EJB(name="ejb/EchoProvider", beanInterface=EchoProviderLocal.class)
>>> public class EchoProviderBean implements EchoProviderLocal {
>>>
>>> public String sayHello(String callerName) {
>>> return "Hello "+callerName;
>>> }
>>> }
>>>
>>> I've deployed the application successfully to glassfish.
>>> When I log in to http://localhost:4848 and go Application Server |
>>> JNDI Browsing, there is only one entry under ejb node that is ejb/mgmt
>>>
>>>
>>> Moreover I've created simple Web Application with one jsp page and a
>>> servlet. When a user clicks button in the browser, the request is
>>> sent to the servlet that calls the following code
>>>
>>> InitialContext ic=new InitialContext();
>>> Object obj=ic.lookup("java:comp/env/ejb/EchoProvider");
>>>
>>> after ic.lookup NamingException is thrown with the message that the
>>> name is not bound.
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>
>>
>>
>
>