users@glassfish.java.net

Custom Resource work when looking it up, but not with injection

From: <glassfish_at_javadesktop.org>
Date: Fri, 11 Dec 2009 05:28:22 PST

Hi All.
I started playing with glassfish v3 and like the new GUI to add custom JNDI Resources. I created a simple example to see how this work, but fail to get the @Resource injection to work, but the normal lookup works fine.

What I did:
1) Set up a custom Resource in the Admin Console:

* JNDI Name: servername
* Resource Type:java.lang.String
* Factory Class:org.glassfish.resources.custom.factory.PrimitivesAndStringFactory
* Property: value=Phillip's local Glassfish v3 Server

Then in the Session Bean:
@Stateless
public class HelloServiceBean{
        
        private Logger logger = Logger.getLogger(getClass().getName());
        
        @Resource(name="servername",type=String.class)
        private String servername;
        
        public String sayHi(String name){
                logger.fine(name + " is greeting you");
                
                logger.fine("Injected servername = " + servername);
                try {
                        InitialContext ic = new InitialContext();
                        String lookupName = (String) ic.lookup("servername");
                        logger.fine("lookupName = " + lookupName);
                        return "Hi " + name + ", this is " + servername + " / " + lookupName;
                } catch (NamingException e) {
                        throw new RuntimeException(e);
                }
        }
}

If I hit the sayHi method with name=dude i get:

This return "Hi dude, this is null / Phillip's local Glassfish v3 Server"

I tried the injecttion with:

@Resource
@Resource(name="servername")
@Resource(name="servername",type=String.class)
@Resource(name="java:global/servername",type=String.class)


Can anyone help me ? What am I missing ?

Thanks in advance
[Message sent by forum member 'phillipkruger' ]

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