users@glassfish.java.net

Re: injecting s.bean X into bean A and passing it to bean B - is that valid

From: <glassfish_at_javadesktop.org>
Date: Tue, 13 May 2008 10:18:50 PDT

Why not simply do a Service Locator:

[code]
public class Locator {
    
    public static SysPropServiceLocal lookupSysPropServiceBean() {
        try {
            Context c = new InitialContext();
            return (SysPropServiceLocal ) c.lookup("java:comp/env/ejb/SysPropServiceBean");
        } catch(NamingException ne) {
            throw new RuntimeException(ne);
        }
    }
}
[/code]

Then:

[code]
public class TxExecutor {
    SysPropServiceLocal serviceBean;

    public TxExecutor() {
        serviceBean = Locator.lookupSysPropServiceBean();
    }

    ...
}
[/code]

Then make sure in any session bean that uses TxExecutor, you need to add:
[code]
    @EJB(name = "ejb/SysPropServiceBean", beanName = "SysPropServiceBean")
    SysPropServiceLocal serviceBean;
[/code]

This ensures that the bean is mapped in to the local JNDI context properly.
[Message sent by forum member 'whartung' (whartung)]

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