【问题标题】:TomEE + Spring Framework + JNDI Lookup FailuresTomEE + Spring Framework + JNDI 查找失败
【发布时间】:2019-03-04 21:36:16
【问题描述】:

我有一个使用 Zulu Java (zulu8.36.0.1-ca-jdk8.0.202-macosx_x64) 部署到 TomEE (tomee-plume-8.0.0-M2) 的简单 Spring Framework (4.3.16.RELEASE) ear 文件.

我通过 JNDI 查找注入数据源时出错。我的 TomEE 实例在 server.xml 中有以下资源:

     <Resource
     auth="Container"
     driverClassName="oracle.jdbc.OracleDriver"
     name="jdbc/my/DataSource"
     password="1234"
     type="javax.sql.DataSource"
     url="jdbc:oracle:thin:@database.local:1521/db"
     username="admin" />

在我的应用程序中,我的 web.xml 文件中有以下内容:

  <servlet>
    <servlet-name>dispatch</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatch</servlet-name>
    <url-pattern>/r/*</url-pattern>
  </servlet-mapping>

我的用于查找 JNDI 资源的 applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    	http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
	<!-- Configures support for @Controllers  -->
    <context:component-scan base-package="local.gerb" />
    <mvc:annotation-driven/>

	<jee:jndi-lookup id="writeDataSource" jndi-name="jdbc/my/DataSource" resource-ref="true"/>

	<bean id="helloWorld" class="local.gerb.HelloWorldImpl" />
    
</beans>

但是,当我部署应用程序时,我在日志文件中收到以下错误:

    CreationException: Error creating bean with name 'writeDataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/my/DataSource] is not bound in this Context. Unable to find [jdbc].
    04-Mar-2019 12:43:33.610 SEVERE [main] org.springframework.web.servlet.DispatcherServlet.initServletBean Context initialization failed
     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'writeDataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/my/DataSource] is not bound in this Context. Unable to find [jdbc].
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1630)

我确实看到在 TomEE 中创建了 jdbc/my/DataSource:

    04-Mar-2019 12:43:32.113 INFO [main] org.apache.tomee.catalina.OpenEJBNamingContextListener.bindResource Importing a Tomcat Resource with id 'jdbc/my/DataSource' of type 'javax.sql.DataSource'.
    04-Mar-2019 12:43:32.113 INFO [main] org.apache.openejb.assembler.classic.Assembler.createRecipe Creating Resource(id=jdbc/my/DataSource)

所以我相信我在 TomEE 中正确创建了资源,但由于某种原因,spring 框架无法正确注入资源。

我确实已将完整源代码推送到我的 github 存储库:https://github.com/jstralko/tomee-poc/tree/master/SpringExamples,以防您想查看所有内容,我将其保持在尽可能少的程度以帮助调试此问题。

任何帮助将不胜感激。

【问题讨论】:

    标签: spring apache-tomee


    【解决方案1】:

    显然,一旦我将资源从 server.xml 移到 context.xml 中,我感到很傻;我能够成功注入 JDBC 资源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 2013-06-29
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多