TomEE + Spring框架+ JNDI查找失败

时间:2019-03-04 21:36:17

标签: spring tomee

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

通过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>

我的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中正确创建了Resource,但是由于某些原因,spring框架无法正确注入资源。

我确实将完整的资源推送到我的github存储库中:https://github.com/jstralko/tomee-poc/tree/master/SpringExamples,以防万一,您需要尽可能减少以帮助调试此问题。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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