【发布时间】:2015-03-05 12:49:25
【问题描述】:
当我尝试使用嵌入式 Tomcat 服务器将 JNDI 数据源与 Spring Boot 和 Spring Data JPA 一起使用时,在使用 SpringApplication.run 运行应用程序时收到以下错误消息:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Instantiation of bean failed;
nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder)] threw exception;
nested exception is org.springframework.jndi.JndiLookupFailureException: JndiObjectTargetSource failed to obtain new target object;
nested exception is javax.naming.NameNotFoundException: Name [comp/env/jdbc/myDataSource] is not bound in this Context. Unable to find [comp].
我使用How to create JNDI context in Spring Boot with Embedded Tomcat Container的解决方案中描述的配置
唯一的区别是对 org.springframework.boot:spring-boot-starter-data-jpa 的附加 Maven 依赖
这是一个示例项目:https://github.com/derkoe/spring-boot-sample-tomcat-jndi(这是解决方案中示例的修改版本)。只需检查、构建并运行 SampleTomcatJndiApplication。
看起来用于查找数据库连接的 JNDI 上下文还不是来自 webapp 的上下文。这似乎是 Spring 上下文和 Tomcat 服务器初始化时的排序问题。
有什么办法解决这个问题吗?
【问题讨论】:
标签: spring-boot embedded-tomcat-8