gpt4 book ai didi

java - 如何设置Spring的DriverManagerDataSource的某些属性?

转载 作者:行者123 更新时间:2023-12-02 02:34:24 27 4
gpt4 key购买 nike

我正在开发一系列教程应用程序来了解不同的 Java EE 技术。我正在遵循各种教程并且或多或少地坚持它们。最近,我开始编写一个简单的 Spring CRUD Web 应用程序来存储、查找、修改和删除 Employee DB 表中的员工。我之前完成了另一个非常相似的应用程序,它仅使用纯 Java 和 Hibernate,并且旨在实现完全相同的功能。该应用程序工作正常,因此我决定将数据库连接设置从这个旧应用程序复制到新的 Spring 应用程序。

问题是,Spring DriverManagerDataSource bean 似乎不接受与原始 Hibernate 配置相同的属性设置,例如“hibernate.hbm2ddl.auto”,我希望在启动时方便地清除数据库,或者“defaultSchema”,Postgres 由于某种原因需要它,所以我一直在配置数据库连接。

如何让 Spring 像旧应用程序中的 Hibernate 一样接受这些属性,并表现出相同的行为?为什么 bean 不以某种可预测的、合理的方式接受这些特定属性,就像其他属性(例如“url”或“password”)一样?我是否应该设置它们,Spring 中是否有其他机制可以处理我想要的属性功能?

旧应用程序配置:

hibernate.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/test2</property>
<property name="connection.username">postgres</property>
<property name="connection.password">postgres</property>

<property name="hibernate.default_schema">public</property>
<property name="show_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>

<mapping class="cz.bsc.hibernatetest.hibernatetutorial.domain.Book" />
<mapping class="cz.bsc.hibernatetest.hibernatetutorial.domain.Author" />

</session-factory>
</hibernate-configuration>

部分 Spring 配置取自教程,我尝试按上述方式进行修改:

spring-servlet.xml

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

<property name="spring.jpa.hibernate.defaultSchema" value="public"></property>
<property name="spring.jpa.hibernate.show_sql" value="true"></property>
<property name="spring.jpa.hibernate.use_sql_comments" value="true"></property>
<property name="spring.jpa.hibernate.hbm2ddl.auto" value="create"></property>
</bean>
</beans>

我的应用程序在当前状态下抛出的完整错误消息。我认为它表明我正在尝试以非预期的方式设置属性,因此存在某种语法错误。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'empController': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dao' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Cannot resolve reference to bean 'jt' while setting bean property 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jt' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Cannot resolve reference to bean 'ds' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ds' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'spring.jpa.hibernate.defaultSchema' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Nested property in path 'spring.jpa.hibernate.defaultSchema' does not exist; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'spring' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Bean property 'spring' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

最佳答案

Hibernate 属性不是数据源定义的一部分。它应该在 session 工厂 bean 下定义。

例如:

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


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource" ref bean="ds" />
<property name="packagesToScan" value="db entities package name" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.defaultSchema">public</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>

关于java - 如何设置Spring的DriverManagerDataSource的某些属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46608903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com