有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将@EJB与远程接口结合使用”

我有glassfish v4和2只耳朵:

  1. 服务。ear包含EJB
  2. WebApplications。ear包含Web 应用程序

我尝试使用:

@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; 
   }

}

共 (2) 个答案

  1. # 1 楼答案

    显然,@EJB(mappedname)的定义与@Stateless(mappedname)定义中的映射名称不同

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

    要实际获得参考,请使用

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

    而不是

    @EJB(mappedName="java:global/Service/allServices/ServiceEJBs!Service1Remote")
    
  2. # 2 楼答案

    我重新安装了glassfish 4.1和JDK1的最新版本。8.0_25,正如马利斯所说的:

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

    而不是

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

    现在它工作得很好

    谢谢Maress:)