У меня есть простой ушной файл Spring Framework (4.3.16.RELEASE), развернутый в TomEE (tomee-plume-8.0.0-M2) с использованием Zulu Java (zulu8.36.0.1-ca-jdk8.0.202-macosx_x64).
Я получаю ошибки при внедрении источника данных через 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)
Я вижу, что jdbc / my / DataSource создается в TomEE:
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 Framework не может правильно внедрить ресурс.
У меня естьполный исходный код добавлен в мой репозиторий github: https://github.com/jstralko/tomee-poc/tree/master/SpringExamples на случай, если вы захотите посмотреть на все, я сохранил его как можно меньше, чтобы помочь отладить эту проблему.
Любая помощь будет принята с благодарностью.
Я чувствую себя глупо, по-видимому, как только я переместил ресурс в context.xml из server.xml;Мне удалось успешно внедрить ресурс JDBC.