从Spring访问EJB类

时间:2016-09-20 13:28:11

标签: java spring spring-mvc tomcat ejb

我有一个使用spring在Java 1.8中构建的Web应用程序,并部署在Tomcat上。 我们使用EJB在Java 1.4中编写了第三方库。

我的应用程序需要从Spring Web应用程序访问第三方库中的方法。

到目前为止,我根据此链接完成了以下操作。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/ejb.html

在我的servlet-context.xml中,我添加了

<beans:bean id="myComponent"
       class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
        <beans:property name="jndiName" value="jndi/ejb/AssociateSessionEJB"/>
        <beans:property name="businessInterface" value="com.company.serviceImpl.MyComponent"/>
    </beans:bean>

 <beans:bean id="loginService" class="com.company.serviceImpl.LoginServiceImpl">
        <beans:property name="myComponent" ref="myComponent"/>
 </beans:bean>

我收到以下错误

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginService' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Cannot resolve reference to bean 'myComponent' while setting bean property 'myComponent'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myComponent' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jndi/ejb/AssociateSessionEJB] is not bound in this Context. Unable to find [jndi].
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:187)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1048)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:570)
    ... 38 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myComponent' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jndi/ejb/AssociateSessionEJB] is not bound in this Context. Unable to find [jndi].
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 52 more

之前我没有使用过EJB。你能告诉我我错过了什么吗?

3 个答案:

答案 0 :(得分:1)

原因在于:

nested exception is javax.naming.NameNotFoundException: Name [jndi/ejb/AssociateSessionEJB] is not bound in this Context. Unable to find [jndi].

为了解决这个问题,您需要将正确的JNDI名称指定为属性jndiName的值。您必须找出JNDI中MyComponent EJB可用的正确名称。

另一件事是使用EJB的应用程序无法部署在作为servlet容器的Tomcat上。你需要使用例如TomEE或其他JEE应用服务器。

答案 1 :(得分:0)

正如您在文档中解释的那样,我认为更简单的方法是将ejb类用作POJO。

{{1}}

答案 2 :(得分:0)

如果您能够在兼容的容器中部署和运行“库”,那么可能的解决方案是,从单独运行的Spring应用程序中使用特定库的远程接口(如果可用)。