EJB 接口 JNDI 查找

Mic*_*tti 5 java dependency-injection ejb jndi

我有一个界面

@Local
public interface TestService extends Serializable
{
    public void aMethod();
}
Run Code Online (Sandbox Code Playgroud)

一个实现它的无状态 bean

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

    @Override
    public void aMethod()
    {
        // do something...
    }
}
Run Code Online (Sandbox Code Playgroud)

托管 bean

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

    @EJB
    private TestService service;

    ...
}
Run Code Online (Sandbox Code Playgroud)

和这个 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");
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

一切都包装好了:

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)
Run Code Online (Sandbox Code Playgroud)

因为我想用common.jar不同的应用程序里面,我想查找基于Testservice接口的名字,就像我注入@EJB TestService service;@ManagedBean@WebServlet

如何实现这一目标?

Mic*_*tti 3

一个肮脏的解决方案是声明

@Stateless(name = "TestService")
public class MyappServiceBean implements TestService
Run Code Online (Sandbox Code Playgroud)

并从 pojo 查找它:

service = (TestService) InitialContext.doLookup("java:module/TestService");
Run Code Online (Sandbox Code Playgroud)

任何更好的解决方案将被愉快地接受!