1

我有例外:

org.springframework.beans.factory.CannotLoadBeanClassException:找不到在 ServletContext 资源 [/WEB-INF/spring-servlet.xml] 中定义的名称为“hibernateTemplate”的 bean 的类 [org.springframework.orm.hibernate.HibernateTemplate];嵌套异常是 java.lang.ClassNotFoundException: org.springframework.orm.hibernate.HibernateTemplate
    在 org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1250)
    在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:576)
    在 org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1319)
    在 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:315)
    在 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:394)
    在 org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:594)
    在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
    在 org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    在 org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    在 org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
    在 org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
    在 org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    在 org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
    在 org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    在 org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
    在 org.apache.catalina.startup.Embedded.start(Embedded.java:825)
    在 org.codehaus.mojo.tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.java:558)
    在 org.codehaus.mojo.tomcat.AbstractRunMojo.execute(AbstractRunMojo.java:255)
    在 org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    在 org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    在 org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    在 org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    在 org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    在 org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    在 org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    在 org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    在 org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    在 org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    在 org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    在 org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    在 org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在 java.lang.reflect.Method.invoke(Method.java:601)
    在 org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    在 org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    在 org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    在 org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
引起:java.lang.ClassNotFoundException:org.springframework.orm.hibernate.HibernateTemplate
    在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
    在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
    在 org.springframework.util.ClassUtils.forName(ClassUtils.java:257)
    在 org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:408)
    在 org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1271)
    在 org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1242)
    ... 39 更多

如何解决?

我的 pom.xml(部分):

    <!--
        Core utilities used by other modules.
        Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*)
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Expression Language (depends on spring-core)
        Define this if you use Spring Expression APIs (org.springframework.expression.*)
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Bean Factory and JavaBeans utilities (depends on spring-core)
        Define this if you use Spring Bean APIs (org.springframework.beans.*)
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Aspect Oriented Programming (AOP) Framework (depends on spring-core, spring-beans)
        Define this if you use Spring AOP APIs (org.springframework.aop.*)
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Application Context (depends on spring-core, spring-expression, spring-aop, spring-beans)
        This is the central artifact for Spring's Dependency Injection Container and is generally always defined
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Various Application Context utilities, including EhCache, JavaMail, Quartz, and Freemarker integration
        Define this if you need any of these integrations
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Transaction Management Abstraction (depends on spring-core, spring-beans, spring-aop, spring-context)
        Define this if you use Spring Transactions or DAO Exception Hierarchy
        (org.springframework.transaction.*/org.springframework.dao.*)
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, spring-tx)
        Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*)
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, and iBatis.
        (depends on spring-core, spring-beans, spring-context, spring-tx)
        Define this if you need ORM (org.springframework.orm.*)
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Object-to-XML Mapping (OXM) abstraction and integration with JAXB, JiBX, Castor, XStream, and XML Beans.
        (depends on spring-core, spring-beans, spring-context)
        Define this if you need OXM (org.springframework.oxm.*)
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Web application development utilities applicable to both Servlet and Portlet Environments
        (depends on spring-core, spring-beans, spring-context)
        Define this if you use Spring MVC, or wish to use Struts, JSF, or another web framework with Spring (org.springframework.web.*)
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Spring MVC for Servlet Environments (depends on spring-core, spring-beans, spring-context, spring-web)
        Define this if you use Spring MVC with a Servlet Container such as Apache Tomcat (org.springframework.web.servlet.*)
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Spring MVC for Portlet Environments (depends on spring-core, spring-beans, spring-context, spring-web)
        Define this if you use Spring MVC with a Portlet Container (org.springframework.web.portlet.*)
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc-portlet</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <!--
        Support for testing Spring applications with tools such as JUnit and TestNG
        This artifact is generally always defined with a 'test' scope for the integration testing framework and unit testing stubs
    -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.0.5.RELEASE</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.10.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.6-Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.6.10.Final</version>
    </dependency>

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
4

1 回答 1

2

您使用了错误的类名。Spring 正在尝试加载:

org.springframework.orm.hibernate.HibernateTemplate

但正确的名称是:

org.springframework.orm.hibernate3.HibernateTemplate

更新您的配置,它应该可以工作。

于 2012-06-07T08:20:25.747 回答