通过@EJB(查找)在其他模块的JSF ManagedBean中注入远程无状态EJB

时间:2015-07-11 13:43:04

标签: java-ee glassfish ejb

不能将EJB从一个模块注入另一个模块中的EJB。这两个模块都在singe glassfish实例上。

主模块中的远程接口:

@Remote
public interface TestEJBLocal {
    String getMessage();

}

在主要模块中实施:

@Stateless
public class TestEJB implements TestEJBRemote {
    @Override
    public String getMessage() {
        return "Hello EJB World";
    }
}

3rdparty模块(消费者)中的远程接口:

@Remote
public interface TestEJBLocal {
    String getMessage();

}

第3方模块中的Consumer bean:

@Named
@RequestScoped
public class testBean {

    @EJB(lookup="java:global/mavenproject3-ear/mavenproject3-ejb-1.0-SNAPSHOT/TestEJB!com.versetty.ejb.TestEJBRemote")
    private TestEJBRemote messageBean;

    public void doEJBCall() {
        System.out.println(messageBean.getMessage());
    }

}

Consumer JSF-page:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        Hello from Facelets
        <h:form>
            <h:commandButton action="#{testBean.doEJBCall()}" title="call"/>
        </h:form>
    </h:body>
</html>

例外:

Caused by: java.lang.IllegalArgumentException: Can not set com.versetty.web.remote.TestEJBRemote field com.versetty.web.bean.testBean.messageBean to com.versetty.ejb._TestEJBRemote_Wrapper
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:688)
... 59 more

1 个答案:

答案 0 :(得分:0)

您使用了@Local接口,但是您尝试从其他EJB应用程序访问它 - 您应该使用@RemoteThis answer给出了原因的一个很好的解释。