简体   繁体   English

在 class 路径资源中定义名称为“dataSource”的 bean 创建错误

[英]Error while creating bean with name 'dataSource' defined in class path resource

I am trying to add postgres database connection to java spring application through JNDI.我正在尝试通过 JNDI 将 postgres 数据库连接添加到 java spring 应用程序。 But while building the application, it shown an error " Error creating bean with name 'dataSource' defined in class path resource ".但是在构建应用程序时,它显示了一个错误“在 class 路径资源中定义名称为 'dataSource' 的 bean 时出错”。

This is my application.properties file.这是我的 application.properties 文件。

spring.datasource.jndi-name=java:/TestDb
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
#spring.jpa.properties.hibernate.dialect = 
org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = none

NOTE: The application is build succesfully if change application.properties like below注意:如果更改 application.properties,应用程序将成功构建,如下所示

spring.datasource.url=jdbc:postgresql://localhost:5444/test
spring.datasource.username=test
spring.datasource.password=test

The stacktrace is given.给出了堆栈跟踪。

 o.s.w.c.s.GenericWebApplicationContext   : Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': 
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; 
nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:/Test'; 
nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, 
or in an application resource file:  java.naming.factory.initial

2021-04-01 13:14:12.383  INFO 14408 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-04-01 13:14:12.398 ERROR 14408 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': 
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: 
Factory method 'dataSource' threw exception; nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: 
Failed to look up JNDI DataSource with name 'java:/Test'; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or 
system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:799) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1356) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1203) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:409) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]
    
    ... 114 common frames omitted

Normally, you dont define a JNDI datasource inside your application, but in your application container .通常,您不会在应用程序中定义JNDI datasource ,而是在应用程序容器中定义。 Spring uses an embedded Tomcat Server though, where JNDI is disabled by default. Spring使用嵌入式 Tomcat 服务器,默认情况下禁用 JNDI。

The easiest way would be to use an external Application Server and modifying its configuration by adding a JNDI configuration.最简单的方法是使用外部应用程序服务器并通过添加JNDI配置来修改其配置。 With embedded tomcat, datasource configuration should be done using the spring.datasource properties.对于嵌入式 tomcat,应使用spring.datasource属性完成数据源配置。

Here you can see how to configure an external Application Server (Tomcat):https://www.baeldung.com/spring-persistence-jpa-jndi-datasource在这里您可以看到如何配置外部应用程序服务器(Tomcat):https://www.baeldung.com/spring-persistence-jpa-jndi-datasource

If you still want to use JNDI with embedded tomcat, you can see here how to do it: How to create JNDI context in Spring Boot with Embedded Tomcat Container如果您仍想将 JNDI 与嵌入式 tomcat 一起使用,您可以在此处查看操作方法: How to create JNDI context in Spring Boot with Embedded Tomcat Container

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在类路径资源+ Springboot中定义名为'dataSource'的bean时出错 - Error creating bean with name 'dataSource' defined in class path resource + Springboot 错误org.springframework.beans.factory.BeanCreationException:创建类路径资源中定义的名称为“ dataSource”的bean时出错 - Error org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource Spriing + hibernate在类路径资源[applicationContext.xml]中创建名称为“ dataSource”的bean时出错 - Spriing+hibernate Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml] Spring 引导 - 创建名称为“dataSource”的 bean 在 class 路径资源中定义时出错 - Spring Boot - Error creating bean with name 'dataSource' defined in class path resource 创建类路径资源中定义的名称为'processEngine'的bean时出错 - Error creating bean with name 'processEngine' defined in class path resource 创建类路径资源中定义的名称为“ sessionFactory”的bean时出错 - Error creating bean with name 'sessionFactory' defined in class path resource 创建名为“springSecurityFilterChain”的 bean 在类路径资源 3 中定义时出错 - Error creating bean with name 'springSecurityFilterChain' defined in class path resource 3 BeanCreationException:创建类路径资源中定义的名称为“ pgService”的bean时出错 - BeanCreationException: Error creating bean with name 'pgService' defined in class path resource 创建在类路径资源中定义的名称为'/ sbhupload'的bean时出错-Junit - Error creating bean with name '/sbhupload' defined in class path resource - Junit 使用在类路径资源中定义的名称“requestMappingHandlerMapping”创建 bean 时出错 - Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM