代码之家  ›  专栏  ›  技术社区  ›  Sargon1

如何设置Spring的DriverManager数据源的某些属性?

  •  0
  • Sargon1  · 技术社区  · 7 年前

    我正在开发一系列教程应用程序来研究不同的Java EE技术。我正在学习各种教程,或多或少会坚持学习。最近,我开始组装一个简单的Spring CRUD webapp,用于在Employee DB表中存储、查找、修改和删除员工。我之前已经完成了另一个非常类似的应用程序,它只使用普通Java和Hibernate,旨在实现完全相同的功能。那个应用程序运行正常,所以我决定将数据库连接设置从这个旧应用程序复制到新的Spring应用程序。

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

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

    冬眠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?
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Yogi    7 年前

    休眠属性不是数据源定义的一部分。 它应该在会话工厂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>