我无法让依赖注入为我的远程服务工作,我无法弄清楚原因。我想要一个RemoteService实例,所以我写了。
@EJB(name="RemoteService")
private RemoteService service;
Bean本身是使用mappedName =“RemoteService:
定义的@Stateless(mappedName = "RemoteService")
public class RemoteServiceBean implements RemoteService
当我尝试运行此代码时,我收到了一个InjectionException:
EJB5070: Exception creating stateless session bean : [{0}]
com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref RemoteService@jndi: service.remote.RemoteService@null@service.remote.RemoteService@Session@null into class service.OrderServiceBean
at com.sun.enterprise.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:387)
at com.sun.enterprise.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:206)
at com.sun.enterprise.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:127)
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:538)
at com.sun.ejb.containers.StatelessSessionContainer.access$100(StatelessSessionContainer.java:111)
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:783)
at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:199)
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:489)
at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:1709)
at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1238)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:195)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:83)
at $Proxy766.size(Unknown Source)
at
我没有ejb配置文件,但我不需要任何配置文件,对吧?远程服务在与尝试引用它的服务相同的glassfish实例上运行。检查glassfish管理员中的JNDI浏览器验证EJB是否已使用正确的jndi名称进行部署,如果我删除@EJB
注释并在构造函数中手动执行查找,它也可以工作:
public OrderServiceBean()
{
try
{
final Properties properties = new Properties();
properties.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
properties.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
properties.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
properties.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
properties.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
final InitialContext initialContext = new InitialContext(properties);
this.service = (RemoteService) initialContext.lookup("RemoteService");
}
想法?
答案 0 :(得分:2)
我无法相信我没有尝试过,但似乎我没有尝试过,我显然没有尝试通过mappedName引用EJB。因此,更改对此的引用可使其有效:
@EJB(mappedName="RemoteService")
private RemoteService service;