我如何将jdbc.properties传递给Spring / Hibernate? [英] How Can I Pass jdbc.properties to Spring/Hibernate?

查看:75
本文介绍了我如何将jdbc.properties传递给Spring / Hibernate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试将属性文件中的值传递给Spring时出现以下错误,因此我不必直接在 hibernate.cfg.xml 中提供它们即可。有没有更好的(和正确的)方法?我知道属性文件被引用,因为如果我输入无效的密码,它就会失败。

I am getting the following error while trying to pass in values from a properties file to Spring, so that I don't have to supply them directly in hibernate.cfg.xml. Is there a better (and correct) approach? I know that the properties file is being referenced because if I put in in invalid password, it fails on that. I'd be grateful for any help.

WARNING: No connection properties specified - the user must supply JDBC connections
Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
    at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
    at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
    at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426)

这是applicationContext.xml:

This is applicationContext.xml:

    <context:component-scan base-package="cmsutil"/>
    <context:property-placeholder location="jdbc.properties"/>

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}"/>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
          p:dataSource-ref="dataSource"
          p:configurationClass="org.hibernate.cfg.AnnotationConfiguration"
          p:packagesToScan="cmsutil.*">

        <property name="exposeTransactionAwareSessionFactory" value="false" />

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
            </props>
        </property>

    </bean>

    <bean id="txnManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory"/>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>          

这是我的hibernate.cfg.xml文件:

This is my hibernate.cfg.xml file:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <mapping resource="cmsutil.entity/contentcomponent.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Per Ramesh的请求 - web.xml

Per Ramesh's request - web.xml

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

对于Ramesh,这是我的jdbc.properties,位于根目录(/ src / java / jdbc.properties)

For Ramesh, this is my jdbc.properties, located in the root directory (/src/java/jdbc.properties)

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://xx:3306/x
jdbc.username=y
jdbc.password=z


hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.generate_statistics=false


推荐答案

在您的 applicationContext.xml中添加此bean

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location"><value>classpath:jdbc.properties</value></property>
    </bean>

在web.xml而不是侦听器中进行更新

update this in web.xml instead of listener

<servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>
         org.springframework.web.context.ContextLoaderServlet
      </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

这篇关于我如何将jdbc.properties传递给Spring / Hibernate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆