我使用Zulu Java (zulu8.36.0.1-ca-jdk8.0.202-macosxx64)将一个简单的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" />
在我的应用程序ear中,我的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,如果你想查看所有内容,我尽可能地将其保持在最小,以帮助调试这个问题。
任何帮助都将不胜感激。
发布于 2019-03-06 03:59:12
显然,当我将资源从server.xml转移到context.xml中时,我觉得自己很愚蠢;我能够成功地注入一个JDBC资源。
https://stackoverflow.com/questions/54991905
复制相似问题