【问题标题】:Spring Connectivity with Postgres DB与 Postgres DB 的 Spring 连接
【发布时间】:2014-11-21 22:05:48
【问题描述】:

尝试连接 Postgres DB,上下文参数如下:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/****" />
<property name="username" value="****" />
<property name="password" value="****" />
</bean>

获取上下文加载异常:

严重:上下文初始化失败 org.springframework.beans.factory.BeanCreationException:错误 创建 ServletContext 中定义的名称为“dataSource”的 bean 资源 [/WEB-INF/ApplicationContext.xml]:错误设置属性 价值观;嵌套异常是 org.springframework.beans.PropertyBatchUpdateException;嵌套的 PropertyAccessExceptions (1) 是: PropertyAccessException 1: org.springframework.beans.MethodInvocationException:属性 'driverClassName' 抛出异常;嵌套异常是 java.lang.IllegalStateException:无法加载 JDBC 驱动程序类 [org.postgresql.Driver]

我在项目的 LIB 中有 postgres 驱动程序,因为我可以检查类文件 org.postgresql.Driver。 Class.forName 还为驱动程序类提供了积极的结果。 我已经尝试了所有版本的 postgres 驱动程序,但仍然没有通过。

【问题讨论】:

  • 你在使用 maven / gradle 吗?你能发布你的 pom / build 文件吗?

标签: java spring postgresql spring-mvc


【解决方案1】:

确保 lib 被正确复制到包装中,并且该目录位于类路径中。

【讨论】:

  • 这一切都已经检查过了,我和其他库一起使用,我正在使用 Spring lib 和 commons lib,目录在类路径中。对此的检查是我使用 Class.forName("org.postgresql.Driver") 证明驱动程序存在于类路径中。任何其他线索,或检查我能做到吗?
【解决方案2】:

我能够解决这个问题,当我将 Postgres DB Jar 添加到 WEBINF/Lib 时,bean 正在创建中,它没有像我之前那样从 JavaResource/Lib 中挑选 jar。

这个问题是一个特殊情况,在大多数地方都没有很好地回答,仅仅检查类路径不会是这个查询的正确答案。

【讨论】:

    猜你喜欢
    • 2021-10-31
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 2014-11-22
    相关资源
    最近更新 更多