如何使用带有远程接口的@EJB”

how can use @EJB with remote interface"

我有 glassfish v4 和 2 个耳朵:

  1. Service.ear 包含 EJB。
  2. WebApplications.ear 包含网页 应用程序。

我尝试使用:

@EJB(mappedName="java:global/Service/allServices/ServiceEJBs!Service1Remote")
Service1Remote service1Remot;

但是我得到了错误:

Caused by: com.sun.faces.spi.InjectionProviderException: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=java:global/Service/allServices/ServiceEJBs!Service1Remote,Remote 3.x interface =Service1Remote,ejb-link=null,lookup=,mappedName=global/Service/allServices/ServiceEJBs!Service1Remote,jndi-name=,refType=Session into class com.manage.application.WebApplication: null

但是当我使用 :

Service1Remote remote= (Service1Remote) new InitialContext().lookup("java:global/Service/allServices/ServiceEJBs!Service1Remote"); 

它工作正常。

EJB:

@Remote
public interface Service1Remote{
   public long getCount(int itemId);
}

@Stateless(name = "ServiceEJBs" , mappedName ="ServiceEJBs")
public Service1Bean implements Service1Remote{
   public long getCount(int itemId){
     ...............
     return 100000999; 
   }

}

很明显,您对@EJB(mappedname) 的定义与@Stateless(mappedNamed) 定义中的映射名称不同。

就是说,如果您替换了正确的映射名称,它甚至不会工作(因为它们在两个不同的 ear 部署中)。

要实际获得参考,请使用

@EJB(lookup="java:global/Service/allServices/ServiceEJBs!Service1Remote")

而不是

@EJB(mappedName="java:global/Service/allServices/ServiceEJBs!Service1Remote")

我重新安装了最新版本的 glassfish 4.1 和 JDK1.8.0_25,并且正如 Maress 所说的那样改变了:

@EJB(lookup="java:global/Service/allServices/ServiceEJBs!Service1Remote")

而不是

@EJB(mappedName="java:global/Service/allServices/ServiceEJBs!Service1Remote")

现在一切正常。

谢谢Maress:)