简体   繁体   中英

How to set certain properties of Spring's DriverManagerDataSource?

I'm working a series of tutorial apps to go over different Java EE technologies. I'm following various tutorials and more or less stick to them. Recently, I started putting together a simple Spring CRUD webapp to store, find, modify, and delete employees in an Employee DB table. I have previously completed another, very similar app, which only used plain Java and Hibernate, and which aimed to achieve the exact same functionality. That app worked alright, so I decided to copy over the database connection settings from this old app to the new, Spring one.

Problem is, the Spring DriverManagerDataSource bean does not seem to accept setting of the same properties as the original Hibernate configuration did, for example "hibernate.hbm2ddl.auto", which I want for conveniently wiping out the DB at startup, or "defaultSchema", which Postgres requires for some reason, so I'm stuck at configuring the DB connection.

How do I get Spring to accept these properties the same as Hibernate in the old app did, and to exhibit the same behavior? Why isn't the bean accepting these particular properties in some predictable, sensible way, like there is for other properties such as "url" or "password"? Am I even supposed to be setting them, isn't there some other mechanism in Spring that takes care of the functionality that I want from the properties?

Old app config:

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>

Part of the Spring config taken from the tutorial which I attempt to modify as described above:

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>

Complete error message that my app in its current shape throws. I presume that it indicates that I'm attempting to set a property in a way that is not intended, so a syntax error of sorts.

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?

Hibernates properties are not part of data-source definition. It should defined under session-factory bean.

for example:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM