users@glassfish.java.net

Re: Mail Session is not injected to member field of my bean

From: <glassfish_at_javadesktop.org>
Date: Mon, 24 Mar 2008 14:10:54 PST

Hi Georg,

> thank you for your reply. In my web.xml I added the
> following:
>
> <resource-ref>
> <description>Mail Session Reference</description>
> <res-ref-name>mail/mobileMailSession</res-ref-name>
> <res-type>javax.mail.Session</res-type>
> <res-auth>Container</res-auth>
> <res-sharing-scope>Shareable</res-sharing-scope>
> <injection-target>
> <injection-target-class>
> sessionbeans.WebSessionBean
> </injection-target-class>
> <injection-target-name>
> mailSession</injection-target-name>
> </injection-target>
> </resource-ref>
>
> This worked and the mail session was injected to the
> member field mailSession.

It might have worked as well even the above <resource-ref> element were not present, because your code looks up by its global jndi-name, and totally bypass the webapp's environment naming context.

>
> Then I tried this one, because I rather would like to
> use annotations:
>
> In my web.xml I now wrote:
>
> <resource-ref>
> <description>Mail Session Reference</description>
> <res-ref-name>mail/mobileMailSession</res-ref-name>
> <res-type>javax.mail.Session</res-type>
> <mapped-name>mail/mobileMailSessionMapped</mapped-name
> >
> </resource-ref>
>
> And in my session bean I declared:
>
> @Resource(name="mail/mobileMailSession",
> mappedName="mail/mobileMailSessionMapped" )
> private javax.mail.Session mailSession;
>
> In this case resource injection still does not work.
>

Your annotation in code and <resource-ref> in web.xml are declaring the same thing. The result is web.xml overriding @Resource annotation with the same stuff.

<resource-ref> in your web.xml is totally redundant, though harmless. Also make sure your web.xml has version attr set to 2.5, or ejb-jar.xml has version 3.0, if you do need them. Otherwise, injection is not supported.

The value of mappedName or mapped-name should be the name you assigned to this resource when you created it in glassfish admin console.

-cheng
[Message sent by forum member 'cf126330' (cf126330)]

http://forums.java.net/jive/thread.jspa?messageID=265567