I am working on an EJB 3 project using OpenEJB (TomEE++).
I have a stateless session bean whose name has been specified with an annotation.
@Stateless(mappedName="SlideService", name="SlideService")
public class SlideService {
public Map<Category, List<SlideShow>> fetchSlideShowsByCategory() {
Application app = Application.getInstance();
return app.retreiveAllSlideShowsByCategory();
}
public SlideShow fetchSlideShow(long id) {
return null;
}
public List<SlideShow> fetchSlideShowsByTitle(String title) {
return null;
}
}
I lookup the Session bean from my Struts action class, like this.
Properties properties = new Properties();
properties.setProperty Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.LocalInitialContextFactory");
InitialContext initialContext = new InitialContext(properties);
SlideService slideService = (SlideService)initialContext.lookup("SlideServiceLocalBean");
In the code above you will notice that I have to use the name 'SlideServiceLocalBean', to locate the bean, whereas I have explicitly specified 'SlideService' in the bean's annotation.
However, when I try to locate the bean in my test case I have to use yet another name string.
EJBContainer ejbContainer = EJBContainer.createEJBContainer();
Object oSlideService = ejbContainer.getContext().lookup("java:global/slides/SlideService");
Why is there a discrepancy in lookup names ?
Am I obtaining the InitialContext in the right way, in the Struts action class, or should I use another mechanism/contextFactory to get the initial context ?
Is it possible to have the bean injected in the action class without using an external DI library ?
Try this inside your struts action class:
It should work fine. So should this from inside the action class:
The lookup using the
LocalInitialContextFactory
predates JavaEE 6 Global JNDI where thesejava:global
andjava:module
names come from.This doc explains how it works.
Your bean's name is
SlideService
and has what is called an@LocalBean
view (a single bean can have many views such as @LocalBean, @Remote, @Local), so the name for looking up the SlideService as an@LocalBean
ends up beingSlideServiceLocalBean
That's just the default format. You can change it to whatever you like in the
conf/system.properties
file.