org.postgresql.Driver 的 bean class [org.springframework.jdbc.datasource.DriverManagerDataSource] 的 属性 'driverClass' 无效

Invalid property 'driverClass' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource] for org.postgresql.Driver

我正在尝试将 JPA SpringData Framework 与 Hibernate 和 PSQL 结合使用,但遇到了这个错误 Whosebug 要我在这里写 smt Whosebug 要我在这里写 smt Whosebug 要我在这里写 smt Whosebug 要我在这里写 smt Whosebug 要我在这里写 smt

 java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [META-INF/ApplicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'driverClass' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Bean property 'driverClass' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222)

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'driverClass' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Bean property 'driverClass' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1076)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:927)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1510)
    ... 44 more

这是我的 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:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/jdbc
                           http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
                           http://www.springframework.org/schema/data/jpa
                           http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    <!-- database -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClass" value="org.postgresql.Driver"></property>
        <property name="jdbcUrl" value="jdbc:postgresql://localhost:5433/journey"></property>
        <property name="user" value="postgres"></property>
        <property name="password" value="canada"></property>
    </bean>
    <!-- entity Manager -->
    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="persistenceUnitName" value="MAP"/>
    </bean>
    <!-- Transaction Manager -->
    <bean id="myTransactionManager"
          class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <!-- JPA package -->
    <jpa:repositories base-package="home.map.events.dao"></jpa:repositories>

</beans>

如果有人有任何建议请评论 post 谢谢

它声称 org.springframework.jdbc.datasource.DriverManagerDataSource 没有名为 driverClass 的 属性 因为 DriverManagerDataSource 只有 属性 driverClassName

查看 documentation

因此您应该将行更改为:

        <property name="driverClassName" value="org.postgresql.Driver"></property>