简体   繁体   English

错误:找不到类AnnotationConfigWebApplicationContext

[英]Error: Class not found AnnotationConfigWebApplicationContext

Hi I am trying to setup a test project with maven + spring 4 + embedded tomcat 嗨,我正在尝试使用Maven + Spring 4 +嵌入式tomcat设置测试项目

I get the following error: 我收到以下错误:

INFO: Starting service Tomcat
Jan 22, 2014 8:27:08 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Jan 22, 2014 8:27:12 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jan 22, 2014 8:27:12 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
08:27:12.127 [localhost-startStop-1] INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization started
08:27:12.141 [localhost-startStop-1] ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.context.ApplicationContextException: Failed to load custom context class [         org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      ]; nested exception is java.lang.ClassNotFoundException:          org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      
    at org.springframework.web.context.ContextLoader.determineContextClass(ContextLoader.java:399) ~[spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:342) ~[spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:280) ~[spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) [tomcat-embed-core-7.0.47.jar:7.0.47]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) [tomcat-embed-core-7.0.47.jar:7.0.47]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-7.0.47.jar:7.0.47]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) [tomcat-embed-core-7.0.47.jar:7.0.47]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) [tomcat-embed-core-7.0.47.jar:7.0.47]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) [na:1.7.0_45]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_45]
    at java.lang.Thread.run(Thread.java:744) [na:1.7.0_45]
Caused by: java.lang.ClassNotFoundException:          org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702) ~[tomcat-embed-core-7.0.47.jar:7.0.47]
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) ~[tomcat-embed-core-7.0.47.jar:7.0.47]
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:238) ~[spring-core-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.context.ContextLoader.determineContextClass(ContextLoader.java:396) ~[spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    ... 12 common frames omitted

I'm not sure why though as I have spring-web as part of my pom 我不确定为什么为什么要在pom中使用spring-web

Hey are my pom files 嘿,我的pom文件

Parent 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>com.ttis.helloworld</groupId>
<artifactId>helloworld-parent</artifactId>
<packaging>pom</packaging>
<version>0.0.1</version>
<name>helloworld parent project</name>
<url></url>


<dependencyManagement>
    <dependencies>

        <!-- This makes sure all spring dependencies including transitive are the same version -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.0.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
             <version>4.0.0.RELEASE</version>
         </dependency>



        <dependency>
            <groupId>com.ttis.helloworld</groupId>
            <artifactId>helloworld-entities</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.ttis.helloworld</groupId>
            <artifactId>helloworld-common</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.ttis.helloworld</groupId>
            <artifactId>helloworld-repositories</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.ttis.helloworld</groupId>
            <artifactId>helloworld-services</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.13</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.0.Final</version>
        </dependency>
    </dependencies>
</dependencyManagement>   


<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>    
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
    </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
    </dependency> 
    <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>  
</dependencies>

<modules>
    <module>helloworld-web</module>
    <module>helloworld-services</module>
    <module>helloworld-entities</module>
    <module>helloworld-common</module>
    <module>helloworld-repositories</module>
</modules>

<properties>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.source>1.7</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties></project>

My web Module: 我的网络模块:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.ttis.helloworld</groupId>
    <artifactId>helloworld-parent</artifactId>
    <version>0.0.1</version>
  </parent>

  <artifactId>helloworld-web</artifactId>
  <packaging>war</packaging>
  <name>helloworld web app</name>

  <dependencies>
      <dependency>
        <groupId>com.ttis.helloworld</groupId>
        <artifactId>helloworld-services</artifactId>
      </dependency>

      <!-- 
      <dependency>
        <groupId>com.ttis.helloworld</groupId>
        <artifactId>helloworld-common</artifactId>
      </dependency>
       -->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
      </dependency>


    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
      </dependency>


    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>helloworld</finalName>


    <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>

                <configuration>

                  <port>9090</port> 

                  <path>/helloworld</path>
                </configuration>


            </plugin>
        </plugins>

  </build>
</project>

My web.xml 我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="
       http://java.sun.com/xml/ns/javaee"
       xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
       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"
    id="helloworld" version="3.0">

  <display-name>Archetype Created Web Application</display-name>

   <context-param>
      <param-name>contextClass</param-name>
      <param-value>
         org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      </param-value>
   </context-param>
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>com.ttis.helloworld.config</param-value>
   </context-param>
   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
 
   <servlet>
      <servlet-name>rest</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <init-param>
         <param-name>contextClass</param-name>
         <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
         </param-value>
      </init-param>
      <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>com.ttis.helloworld.config</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>

</web-app>

Can anyone help me figure out what I might be missing? 谁能帮我弄清楚我可能会缺少什么?

Add this dependency to your POM.xml 将此依赖项添加到您的POM.xml

      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.0.RELEASE</version>
      </dependency>

Change the version to, which you work!. 将版本更改为您可以使用的版本!。 re-run mvn clean install Hope this helps 重新运行mvn clean install希望这会mvn clean install帮助

Sadly it was white-spaces in the web.xml file 可悲的是,web.xml文件中的空格

Example: 例:

<param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value>

and the like 等等

cause of problem: 问题原因:

Caused by: java.lang.ClassNotFoundException:          org.springframework.web.context.support.AnnotationConfigWebApplicationContext

you can find this class in that jars: 您可以在这些罐子中找到此类:

http://mavenhub.com/c/org/springframework/web/context/support/annotationconfigwebapplicationcontext/jar http://mavenhub.com/c/org/springframework/web/context/support/annotationconfigwebapplicationcontext/jar

if you use maven you can to add correspond dependency. 如果使用maven,则可以添加对应的依赖项。

Hence this configuration is impossible for spring 4 因此,这种构造对于弹簧4是不可能的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM