users@glassfish.java.net

Re: Problem on configuring Java Mail Session

From: Sahoo <sahoo_at_sun.com>
Date: Thu, 20 Mar 2008 23:51:45 +0530

While using @Resource at the class level, use "type" field of @Resource
to indicate the type of the resource. The javadoc of @Resource says the
following:

    /**
     * The Java type of the resource. For field annotations,
     * the default is the type of the field. For method annotations,
     * the default is the type of the JavaBeans property.
     * For class annotations, there is no default and this must be
     * specified.
     */
    Class type() default java.lang.Object.class;

So, you should do something like this:

@Resource(name="mail/MailSession3", type=Session.class,
mappedName="mail/FossDemoMailSession")

Without the type attribute, @Resource is ignored. If you run verifier
against such an app, it will report a nice error.

Thanks,
Sahoo

glassfish_at_javadesktop.org wrote:
> You sample code works because the @Resource(name="mail/MailSession", mappedName="...") has been injected to the Session object, as such it is "recognized" as MailSession in this context...
>
> Could you please try to move @Resource(name="mail/MailSession", mappedName=...) up to the class level, and then look it inside the class,
> e.g.
> @Resource(name="mail/MailSession", mappedName="FossDemoMailSession")
> @Stateless
>
> public class VisitorRegistrationService{
> ....
> void someMethod{
> InitialContext ctx = new InitialContext();
> Object a = ctx.lookup("mail/FossDemoMailSession");
> Object b = ctx.lookup("java:comp/env/mail/MailSession");
> ...
> You will get that ClassCastException!
>
> Please note that, I changed the SessionEJB from WebService, because I was experiencing problem when I annotated @Resource on the class level, the MailSession could not be injected even it is legal according to the EJB 3 Spec...
> [Message sent by forum member 'dabaner' (dabaner)]
>
> http://forums.java.net/jive/thread.jspa?messageID=264975
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>