【问题标题】:ClassNotFoundException (class PostgreSQLDialect not found) when trying to deploy Spring app on Tomcat server尝试在 Tomcat 服务器上部署 Spring 应用程序时出现 ClassNotFoundException(找不到类 PostgreSQLDialect)
【发布时间】:2014-02-03 19:39:46
【问题描述】:

当我尝试在 Tomcat 服务器上运行我的项目时,我收到以下错误:

java.lang.ClassNotFoundException: org.hibernate.dialect.PostgreSQLDialect 

完整的错误堆栈:

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private family.service.ContactService family.controller.ContactController.contactService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private family.dao.ContactDAO family.service.ContactServiceImpl.contactDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory family.dao.ContactDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.PostgreSQLDialect 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
...
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private family.service.ContactService family.controller.ContactController.contactService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private family.dao.ContactDAO family.service.ContactServiceImpl.contactDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory family.dao.ContactDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.PostgreSQLDialect 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 29 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private family.dao.ContactDAO family.service.ContactServiceImpl.contactDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory family.dao.ContactDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.PostgreSQLDialect 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
...
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private family.dao.ContactDAO family.service.ContactServiceImpl.contactDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory family.dao.ContactDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.PostgreSQLDialect 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 42 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory family.dao.ContactDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.PostgreSQLDialect 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory family.dao.ContactDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.PostgreSQLDialect 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 55 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.PostgreSQLDialect 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1548)
...
Caused by: org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.PostgreSQLDialect 
    at org.hibernate.dialect.resolver.DialectFactory.constructDialect(DialectFactory.java:159)
    at org.hibernate.dialect.resolver.DialectFactory.buildDialect(DialectFactory.java:99)
...
Caused by: java.lang.ClassNotFoundException: org.hibernate.dialect.PostgreSQLDialect 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
...

当我尝试遵循本教程时遇到了这个问题: Tutorial:Create Spring 3 MVC Hibernate 3 Example using Maven in Eclipse

我正在使用 Hibernate 3.6.9-Final,Spring 4。(如果重要,我可以切换到 Spring 3。我只是从另一个项目中获取了 Spring 依赖项。)

我尝试过的:

  • 从 Google 搜索我是否缺少 Maven 依赖项。 org.hibernate.dialect 类似乎是由 Hibernate 提供的,我的 pom.xml 中有 hibernate-core。我尝试将 hibernate-entitymanager、hibernate-tools 等添加到 pom.xml 中,希望其中之一提供必要的依赖,但没有帮助。
  • 我想也许我需要添加 PostgreSQL 驱动程序,所以我在 pom.xml 中添加了 9.1-901-1.jdbc4(见下文)。它没有效果。
  • 我尝试查看 Eclipse 是否可以解析 org.hibernate.dialect.PostgreSQLDialect 类,确实可以。 (我拿了随机项目文件并创建了 PostgreSQLDialect 的实体字段,然后 CTRL+单击该字段,我被带到了类 org.hibernate.dialect.PostgreSQLDialect 的源代码)。但是当我尝试在服务器上运行该项目时,我仍然会遇到错误。
  • 按照here 的指示,我尝试检查 Maven 依赖树。我没有从看它变得更聪明,但是在这篇文章的末尾提供了它,也许它可以帮助某人回答。

spring-servlet.xml

按照教程中的说明,我创建了 spring-servlet.xml:

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config />
    <context:component-scan base-package="family" />

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
        p:password="${jdbc.password}" />

     <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

jdbc.properties

另外,我创建了 jdbc.properties,其中包括以下行(其他行未提供,因为它们包含密码):

jdbc.dialect=org.hibernate.dialect.PostgreSQLDialect 

pom.xml

我有以下 POM 文件(因为本教程没有提供 POM 文件,所以我尽可能将不同来源的 POM 文件组合在一起):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>0.5.0.M6</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.9.Final</version>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <!-- <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.0</version> 
                </plugin> -->
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

maven 依赖树

mvn dependency:tree -Dverbose

[INFO] test:test:war:0.0.1-SNAPSHOT
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] +- junit:junit:jar:4.11:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- javax.persistence:persistence-api:jar:1.0:compile
[INFO] +- org.springframework:spring-context:jar:4.0.0.RC1:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.0.0.RC1:compile
[INFO] |  |  +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate
)
[INFO] |  |  +- (org.springframework:spring-beans:jar:4.0.0.RC1:compile - omitte
d for duplicate)
[INFO] |  |  \- (org.springframework:spring-core:jar:4.0.0.RC1:compile - omitted
 for duplicate)
[INFO] |  +- org.springframework:spring-beans:jar:4.0.0.RC1:compile
[INFO] |  |  \- (org.springframework:spring-core:jar:4.0.0.RC1:compile - omitted
 for duplicate)
[INFO] |  +- org.springframework:spring-core:jar:4.0.0.RC1:compile
[INFO] |  \- org.springframework:spring-expression:jar:4.0.0.RC1:compile
[INFO] |     \- (org.springframework:spring-core:jar:4.0.0.RC1:compile - omitted
 for duplicate)
[INFO] +- org.springframework:spring-orm:jar:4.0.0.RC1:compile
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- (org.springframework:spring-beans:jar:4.0.0.RC1:compile - omitted f
or duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:4.0.0.RC1:compile - omitted fo
r duplicate)
[INFO] |  +- org.springframework:spring-jdbc:jar:4.0.0.RC1:compile
[INFO] |  |  +- (org.springframework:spring-beans:jar:4.0.0.RC1:compile - omitte
d for duplicate)
[INFO] |  |  +- (org.springframework:spring-core:jar:4.0.0.RC1:compile - omitted
 for duplicate)
[INFO] |  |  \- (org.springframework:spring-tx:jar:4.0.0.RC1:compile - omitted f
or duplicate)
[INFO] |  \- (org.springframework:spring-tx:jar:4.0.0.RC1:compile - omitted for
duplicate)
[INFO] +- org.springframework:spring-webmvc:jar:4.0.0.RC1:compile
[INFO] |  +- (org.springframework:spring-beans:jar:4.0.0.RC1:compile - omitted f
or duplicate)
[INFO] |  +- (org.springframework:spring-context:jar:4.0.0.RC1:compile - omitted
 for duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:4.0.0.RC1:compile - omitted fo
r duplicate)
[INFO] |  +- (org.springframework:spring-expression:jar:4.0.0.RC1:compile - omit
ted for duplicate)
[INFO] |  \- (org.springframework:spring-web:jar:4.0.0.RC1:compile - omitted for
 duplicate)
[INFO] +- org.springframework:spring-tx:jar:4.0.0.RC1:compile
[INFO] |  +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-beans:jar:4.0.0.RC1:compile - omitted f
or duplicate)
[INFO] |  \- (org.springframework:spring-core:jar:4.0.0.RC1:compile - omitted fo
r duplicate)
[INFO] +- org.springframework:spring-web:jar:4.0.0.RC1:compile
[INFO] |  +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-aop:jar:4.0.0.RC1:compile - omitted for
 duplicate)
[INFO] |  +- (org.springframework:spring-beans:jar:4.0.0.RC1:compile - omitted f
or duplicate)
[INFO] |  +- (org.springframework:spring-context:jar:4.0.0.RC1:compile - omitted
 for duplicate)
[INFO] |  \- (org.springframework:spring-core:jar:4.0.0.RC1:compile - omitted fo
r duplicate)
[INFO] +- org.hibernate:hibernate-core:jar:3.6.9.Final:compile
[INFO] |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.1:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:compile

[INFO] |  |  \- (org.slf4j:slf4j-api:jar:1.7.5:compile - version managed from 1.
6.1; omitted for duplicate)
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Fin
al:compile
[INFO] |  +- javax.transaction:jta:jar:1.1:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.5:compile
[INFO] +- postgresql:postgresql:jar:9.1-901-1.jdbc4:compile
[INFO] +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- commons-dbcp:commons-dbcp:jar:1.4:compile
[INFO] |  \- commons-pool:commons-pool:jar:1.5.4:compile
[INFO] +- jstl:jstl:jar:1.2:compile
[INFO] +- org.mockito:mockito-core:jar:1.9.5:test
[INFO] |  +- (org.hamcrest:hamcrest-core:jar:1.1:test - omitted for conflict wit
h 1.3)
[INFO] |  \- org.objenesis:objenesis:jar:1.0:test
[INFO] \- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO]    \- (org.hamcrest:hamcrest-core:jar:1.3:test - omitted for duplicate)

更新:

Al Sweetman 提出问题在于 JAR-s 没有被打包到 WAR 中。我们与一位同事核实:

  1. 我在项目上运行了“mvn package”(从命令行)
  2. 我将创建的 WAR (test-1.0.0-BUILD-SNAPSHOT.war) 放入 Tomcat (C:\apache-tomcat-7.0.50\webapps)
  3. 我从命令行运行了 startup.bat

在 Tomcat 日志中,我看到同样的错误:找不到 PostgreSQLDialect。

Tomcat 已解压 WAR 文件。如果我查看目录: C:\apache-tomcat-7.0.50\webapps\test-1.0.0-BUILD-SNAPSHOT\WEB-INF\lib

我看到 postgresql-9.1-901-1.jdbc4.jar 文件很好地捆绑在一起。我不知道为什么项目在读取 spring-servlet.xml 时找不到 JAR 文件...

更新 2: 所有必要的 JAR 似乎都存在:

更新3: 我将 PostgreSQL 数据库更改为 H2 数据库,现在它开始工作而没有错误。

我还从 Run Configurations -> Classpath 中删除了 JAR,因为现在它们似乎没有任何效果。

但是,如果我从部署程序集中删除 Maven 依赖项,则会收到错误消息: java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener。所以这也是解决方案的一部分。无论如何,现在一切正常(除非我想再次使用 PostgreSQL :))

【问题讨论】:

  • hibernate jar 文件是否存在?

标签: spring hibernate maven tomcat spring-mvc


【解决方案1】:

假设您在 Eclipse/STS 中运行服务器:值得检查服务器启动配置以确保 postgres jar 作为依赖项附加。我经常发现“编辑器”类路径和“服务器运行时”类路径之间存在差异。

  1. 点击“运行”
  2. 点击“运行配置”
  3. 单击“Apache Tomcat”并选择您的配置/服务器
  4. 单击“类路径”选项卡。然后,您可以尝试在选择“用户条目”后通过“添加罐子”按钮手动添加 postgres 罐子

另一个想法

还有另一个可以使用的配置——我想我可能得到了错误的 Eclipse 窗口(通常是 IntelliJ 用户)。

试试下面的

  1. 右键单击 Eclipse 项目
  2. 选择“属性”
  3. 选择“部署程序集”
  4. 检查 JAR 列表以获取 hibernate 和 postgres。
  5. 如果没有,请点击“添加”
  6. 选择“通过路径变量归档”
  7. 选择“M2_REPO”,然后从下拉列表中找到您的 repo 中用于 postgres 和 hibernate 的 jar。

然后点击完成。

我认为发生的事情是 jar 文件在战争创建时没有被捆绑到战争中。

【讨论】:

  • 当我点击“添加 JAR”时,我只能从项目中添加 JAR。我改为单击“添加外部 JAR”并可以从文件系统添加。我从mvnrepository.com/artifact/postgresql/postgresql/… 添加了 postgresql-9.1-901-1.jdbc4.jar。这是你的意思吗?我仍然遇到同样的错误。
  • 这就是我的意思,我很抱歉。你也可以尝试添加hibernate jar文件吗?
  • 将 hibernate.jar 添加到类路径似乎有所帮助:我现在遇到另一个错误:) 我也会尝试解决这个问题,如果我不能,我会提出新问题。严重:Servlet /test 抛出 load() 异常 java.lang.ClassNotFoundException: org.dom4j.DocumentException
  • 另一个想法 - 我想我可能得到了错误的 Eclipse 窗口(通常是 IntelliJ 用户)。尝试以下操作 1. 右键单击​​ Eclipse 项目 2. 选择“属性” 3. 选择“部署程序集” 4. 检查 JAR 列表中的 hibernate 和 postgres。 5. 如果不存在,请单击“添加” 6. 选择“通过路径变量存档” 7. 选择“M2_REPO”并从下拉列表中找到您的存储库中用于 postgres 和休眠的 jar。然后点击完成。我认为发生的事情是 jar 文件在战争创建时没有被捆绑到战争中。
  • 这一切都让我很困惑,因为我是 Java EE 世界的新手。我尝试了替代变体(从运行配置中删除了 JAR 并将它们添加到部署程序集),但后来我得到了与以前相同的错误(找不到 PostgreSQLDialect)。现在我将手动下载的 JAR 添加回运行配置并将它们从部署程序集中删除,我收到新错误:java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.ReflectionManager。这很奇怪,因为刚才我遇到了找不到 org.dom4j.DocumentException 的错误!
【解决方案2】:

Al Sweetman 的建议有所帮助,但我得到了新的错误和错误 (ClassNotFoundExceptions)。

所以除了 Al Sweetman 的建议之外,我还向 Java 构建路径添加了 Maven 依赖项。 之后我没有得到任何错误!其实我错了。我看着服务器开始工作,但它仍然抛出同样的错误。将 Maven 依赖项添加到 Java 构建路径没有任何效果。

更新 我将 PostgreSQL 数据库更改为 H2 数据库,现在一切正常。但我仍然需要将 Maven 依赖项添加到部署程序集,所以这是解决方案的一部分。

【讨论】:

  • 啊,好一个@Rauni!干得好先生。只需检查我的项目 - 我在订单/导出中也有那些 Maven 依赖项。如果您将它们保留为“导出”并从部署程序集中删除条目,我将有兴趣看看会发生什么。打赌你现在感觉很满意...... :)
  • 嗯,@AlSweetman,它没有帮助 :) 看到我的编辑。我与一位同事一起调查了这个问题,我将用我们发现的一些信息更新我的原始帖子。
猜你喜欢
  • 2012-12-18
  • 2019-10-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多