问题描述
我无法获得依赖注入为我的远程服务工作,我无法弄清楚为什么。我想要一个RemoteService的实例,所以我写了。
@EJB(name =RemoteService)
私有RemoteService服务;
并且Bean本身使用mappedName =RemoteService:
定义 @Stateless(mappedName =RemoteService)
public class RemoteServiceBean实现RemoteService
当我尝试运行此代码时,我得到一个InjectionException:
EJB5070:创建无状态会话Bean的异常:[{0}]
com.sun.enterprise.InjectionException:尝试注入未解析的Ejb-Ref RemoteService @ jndi的异常:service.remote.RemoteService @ null @ service.remote.RemoteService @ Session @null入类service.OrderServiceBean
在com.sun.enterprise.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:387)
在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)
在com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:1709)
在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(未知来源)
在
我没有ejb配置文件,但我不需要任何,对吧?远程服务运行在与尝试引用它的服务相同的glassfish实例上。检查glassfish admin中的JNDI浏览器验证是否使用正确的jndi名称部署了EJB,如果我删除了 @EJB
注释并在构造函数中手动执行查找,作品:
public OrderServiceBean()
{
try
{
final属性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);
}
想法
我不敢相信我没有尝试过,但似乎没有,我显然没有尝试通过mappedName引用EJB。所以改变这个引用使它可以工作:
$ $ p $ code $ @EJB(mappedName =RemoteService)
私有RemoteService服务;
I am unable to get dependency injection to work for my remote service and I cannot figure out why. I want an instance of RemoteService so I wrote.
@EJB(name="RemoteService")
private RemoteService service;
And the Bean itself is defined with mappedName="RemoteService:
@Stateless(mappedName = "RemoteService")
public class RemoteServiceBean implements RemoteService
When I try to run this code I get an InjectionException:
EJB5070: Exception creating stateless session bean : [{0}]
com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref RemoteService@jndi: service.remote.RemoteService@[email protected]@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
I have no ejb config files but I shouldn't need any, right? The remote service is running on the same glassfish instance as the service trying to reference it. Checking the JNDI browser in glassfish admin verifies that the EJB has been deployed with the correct jndi name and if I remove the @EJB
annotation and do the lookup manually in the constructor it also works:
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");
}
Ideas?
I can't believe I hadn't tried it but it seems I hadn't, I obviously hadn't tried referencing the EJB through mappedName. So changing the reference to this makes it work:
@EJB(mappedName="RemoteService")
private RemoteService service;
这篇关于Glassfish上远程服务的EJB依赖注入失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!