EJB JNDI lookup interface

I have an interface

@Local
public interface TestService extends Serializable
{
    public void aMethod();
}

party bean that implements it

@Stateless
public class MyappServiceBean implements TestService
{
    private static final long serialVersionUID = 1L;

    @Override
    public void aMethod()
    {
        // do something...
    }
}

driven bean

@ManagedBean
public class MyappBean implements Serializable
{
    private static final long serialVersionUID = 1L;

    @EJB
    private TestService service;

    ...
}

and this is pojo

public class TestPojo implements Serializable
{
    private static final long serialVersionUID = 1L;

    private final TestService service;

    public TestPojo()
    {
        // this works
        // service = (TestService) InitialContext.doLookup("java:global/myapp/MyappServiceBean");

        // this works too
        // service = (TestService) InitialContext.doLookup("java:global/myapp/MyappServiceBean!com.example.common.ejb.TestService");

        // this works again
        // service = (TestService) InitialContext.doLookup("java:app/myapp/MyappServiceBean");

        // how to lookup ONLY by interface name/class ?
        service = (TestService) InitialContext.doLookup("???/TestService");
    }

    ...
}

All packages are packed:

myapp.war
    |   
    + WEB-INF
        |
        + lib
        |   |
        |   + common.jar
        |       |
        |       - com.example.common.ejb.TestService (@Local interface)
        |       |
        |       - com.example.common.util.TestPojo (simple pojo, must lookup)
        |
        + classes
            |
            - com.example.myapp.ejb.MyappServiceBean (@Stateless impl)
            |
            - com.example.myapp.jsf.MyappBean (jsf @ManagedBean)

since I want to use common.jarinside different applications, I want to search based on the name of the interface Testservice, just as I insert @EJB TestService service;into @ManagedBeanor@WebServlet

how to achieve this?

+4
source share
1 answer

the dirty decision is to declare

@Stateless(name = "TestService")
public class MyappServiceBean implements TestService

and find it from pojo:

service = (TestService) InitialContext.doLookup("java:module/TestService");

any best decision will be happy to make!

+2
source

Source: https://habr.com/ru/post/1526032/


All Articles