我有 glassfish v4 和 2 只耳朵:
- Service.ear 包含 EJB。
- 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;
}
}
最佳答案
显然,您对 @EJB(mappedname) 的定义与 @Stateless(mappedNamed) 定义中的映射名称不同。
也就是说,如果您替换了正确的映射名称,它甚至不会工作(因为它们位于两个不同的耳朵部署中)。
要真正获得引用,请使用
@EJB(lookup="java:global/Service/allServices/ServiceEJBs!Service1Remote")
而不是
@EJB(mappedName="java:global/Service/allServices/ServiceEJBs!Service1Remote")
关于java - 如何通过远程接口(interface)使用@EJB”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28019992/