尝试在Tomcat服务器上部署Spring应用时发生ClassNotFoundException(找不到类PostgreSqlDialogue)
当我尝试在Tomcat服务器上运行项目时,出现以下错误:尝试在Tomcat服务器上部署Spring应用时发生ClassNotFoundException(找不到类PostgreSqlDialogue),spring,hibernate,maven,tomcat,spring-mvc,Spring,Hibernate,Maven,Tomcat,Spring Mvc,当我尝试在Tomcat服务器上运行项目时,出现以下错误: java.lang.ClassNotFoundException: org.hibernate.dialect.PostgreSQLDialect 完整错误堆栈: SEVERE: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactCont
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)
...
当我尝试遵循本教程时,我遇到了这个问题:
我正在使用Hibernate3.6.9-Final,Spring4。(如果有必要的话,我可以切换到Spring 3。我只是从另一个项目中获取了Spring依赖项。)
我所尝试的:
- 从Google搜索我是否缺少Maven依赖项。类org.hibernate.dial似乎是由hibernate提供的,我的pom.xml中有hibernate核心。我试图将hibernate entitymanager、hibernate工具等添加到pom.xml中,希望其中一个能够提供必要的依赖性,但没有帮助
- 我想可能需要添加PostgreSQL驱动程序,所以我在pom.xml中添加了9.1-901-1.jdbc4(见下文)。没有效果
- 我试图看看Eclipse是否能够解析org.hibernate.dialent.PostgreSqlDialent类,事实上,它能够。(我获取了随机项目文件,创建了PostgreSqlDialogue的实体字段,并按住CTRL键单击该字段,然后我被带到了org.hibernate.dialogue.PostgreSqlDialogue类的源代码中)。但当我尝试在服务器上运行项目时,仍然会出现错误
- 我试着按照指示检查Maven依赖树。我并没有因为看了它而变得更聪明,但它是在这篇文章的末尾提供的,也许它能帮助别人回答
<?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>
pom.xml
我有以下POM文件(因为教程没有提供POM文件,所以我尽可能地从不同的来源组合POM文件):
更新:
阿尔·斯威特曼提出,问题在于JAR-s并没有将包裹投入战争。我们与一位同事进行了核实:
java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener。所以这也是解决方案的一部分。无论如何,现在一切都正常了(除非我想再次使用PostgreSQL:)假设您正在Eclipse/STS中运行服务器:检查服务器启动配置以确保postgres jar作为依赖项连接是值得的。我经常发现“编辑器”类路径和“服务器运行时”类路径之间存在差异
我认为发生的事情是,jar文件在战争创建时没有被捆绑到战争中。Al Sweetman的建议有所帮助,但我得到了新的错误和错误(ClassNotFoundExceptions) 所以除了Al Sweetman的建议之外,我还向Java构建路径添加了Maven依赖项。之后,我没有得到任何错误!事实上我错了。我看到服务器开始工作,但它仍然抛出相同的错误。将Maven依赖项添加到Java构建路径没有任何效果 更新 我将PostgreSQL数据库更改为H2数据库,现在一切正常。但我仍然需要向部署程序集添加Maven依赖项,所以这是解决方案的一部分
当我单击“添加jar”时,我只能从项目中添加jar。我改为单击“添加外部JAR”,并可以从文件系统中添加。我从中添加了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。检查hibernate和postgres的JAR列表。5.如果没有,请单击“添加”6。选择“通过路径变量存档”7。选择“M2_REPO”,然后从下拉列表中查找postgres和hibernate的REPO中的jar。
jdbc.dialect=org.hibernate.dialect.PostgreSQLDialect
<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>
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)