Skip to main content

@Resource for MailSession in JSF Container

4 replies [Last post]
dmitrys
Offline
Joined: 2007-07-09

Hi,

I'm trying to use the @Resource annotation to inject the Mail Session pre created in Glassfish.
The JNDI name is mail/testmail (as it shown in the JNDI Browser under mail node).

annotation in Managed Bean used like this:

@Resource(name = "mail/testmail")
javax.mail.Session mailSession;

The mailSession is never initialized, looks like the @Resource annotation is ignored by JSF Container.

I tried to use the JNDI Lookup for name java:comp/env/mail/testmail but null is returned..

Is it implemented in Glassfish?

Thanks,
Dmitry.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cf126330
Offline
Joined: 2005-03-29

Hi Dmitry,

Did you map the resource reference name mail/testmail (as specified in the name attr) to the global jndi name mail/testmail (as seen in the jndi browser)?

You can try:
@Resource(name = "mail/testmail", mappedName="mail/testmail")
javax.mail.Session mailSession;

Or you can map it in sun-web.xml

-cheng

dmitrys
Offline
Joined: 2007-07-09

Hi Cheng,

Thanks a Lot for the pointer! When the JNDI name specified in the web.xml :


Mail Session Reference
mail/testmail
javax.mail.Session
Container

JSF Managed Bean can use the JNDI Lookup to get the reference, but @Resource (even with mappedName specified) is still playing comment... and can't find any indication if annotation handler was called at all or not:(

Thanks again,
Dmitry.

cf126330
Offline
Joined: 2005-03-29

Hi Dmitry,

Can you check if your managed bean has been properly configured as a managed bean. Otherwise, injections in that class will simply be ignored.

Check WEB-INF/faces-config.xml for these lines:

xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">


yourBean
com.xxx.xxx.YourBean
request

Also check your web.xml. It should use servlet version 2.5 to support annotation processing:


javax.faces.CONFIG_FILES /WEB-INF/faces-config.xml

-cheng

dmitrys
Offline
Joined: 2007-07-09

Cheng Man, you just saved my belief in EJB 3 :)
Now i can see the annotations are working and container trying to inject things...
I had web.xml generated by IDEA and somehow the version was not 2.5 but 2.4, probably didn't specified the correct one when started to play with it (yesterday)....

It will be nice to have some indication in the log handling such case (instead of silent ignore).

Thanks A LOT,
Dmitry.