Linked Questions

Popular Questions

Which InitialContextFactory should I use?

Asked by At

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 ?

Related Questions