簡體   English   中英

如何設置Spring的DriverManagerDataSource的某些屬性?

[英]How to set certain properties of Spring's DriverManagerDataSource?

我正在研究一系列教程應用程序,以介紹不同的Java EE技術。 我正在關注各種教程,或多或少地堅持使用它們。 最近,我開始組裝一個簡單的Spring CRUD Web應用程序,以在Employee DB表中存儲,查找,修改和刪除員工。 我之前已經完成了另一個非常相似的應用程序,該應用程序僅使用普通的Java和Hibernate,並且旨在實現完全相同的功能。 該應用程序運行良好,因此我決定將數據庫連接設置從該舊應用程序復制到新的Spring 1。

問題是,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?

休眠屬性不是數據源定義的一部分。 它應該在會話工廠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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM