如何将jdbc.properties传递给Spring/Hibernate?

如何将jdbc.properties传递给Spring/Hibernate?,spring,hibernate,Spring,Hibernate,我在尝试将属性文件中的值传递给Spring时遇到以下错误,这样我就不必直接在hibernate.cfg.xml中提供它们。有更好(正确)的方法吗?我知道正在引用属性文件,因为如果我输入了无效密码,它将失败。如果有任何帮助,我将不胜感激 WARNING: No connection properties specified - the user must supply JDBC connections Exception in thread "main" org.hibernate.Hiberna

我在尝试将属性文件中的值传递给Spring时遇到以下错误,这样我就不必直接在hibernate.cfg.xml中提供它们。有更好(正确)的方法吗?我知道正在引用属性文件,因为如果我输入了无效密码,它将失败。如果有任何帮助,我将不胜感激

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:

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

将此bean添加到
applicationContext.xml

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

类路径:jdbc.properties
在web.xml中更新此文件,而不是在侦听器中更新

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

上下文
org.springframework.web.context.ContextLoaderServlet
1.

我也遇到了同样的问题,但这是我的java代码中出现错误的结果。在调用
buildSessionFactory()
之前,需要调用
配置

configure()
方法的javadoc:

使用应用程序资源中指定的映射和属性 名为hibernate.cfg.xml

断开的代码:

public static void main(String[] args) {
        Configuration cfg = new Configuration();

        factory = cfg.buildSessionFactory(); // missing the configure() call

        Session s = factory.openSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();
public static void main(String[] args) {
        Configuration cfg = new Configuration();

        factory = cfg.configure().buildSessionFactory();

        Session s = factory.openSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();
解决方案:

public static void main(String[] args) {
        Configuration cfg = new Configuration();

        factory = cfg.buildSessionFactory(); // missing the configure() call

        Session s = factory.openSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();
public static void main(String[] args) {
        Configuration cfg = new Configuration();

        factory = cfg.configure().buildSessionFactory();

        Session s = factory.openSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();

注意:我没有使用Spring配置。

谢谢你的建议。我试过了,但还是犯了同样的错误。非常令人沮丧。这一定比我做的要简单。根据您的请求添加了web.xml。谢谢。你的jdbc.properties在哪里请参见上面的jdbc.properties。它应该在classpath means src文件夹中。
public static void main(String[] args) {
        Configuration cfg = new Configuration();

        factory = cfg.configure().buildSessionFactory();

        Session s = factory.openSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();