TomEE + Spring Framework + JNDI查找失败

问题描述 投票:0回答:1

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

我通过JNDI Lookup注入数据源时遇到错误。我的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>

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

<?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 repo:https://github.com/jstralko/tomee-poc/tree/master/SpringExamples,以防你想要查看所有内容,我尽可能保持最小化以帮助调试此问题。

任何帮助将不胜感激。

spring tomee
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.