错误:未找到类AnnotationConfigWebApplicationContext

时间:2014-01-22 12:51:34

标签: java spring maven

您好我正在尝试使用maven + spring 4 +嵌入式tomcat设置测试项目

我收到以下错误:

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

我不知道为什么,因为我有弹簧网作为我的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>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>

我的网络模块:

<?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>

我的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>

任何人都可以帮我弄清楚我可能缺少什么吗?

3 个答案:

答案 0 :(得分:1)

将此依赖项添加到POM.xml

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

将版本更改为您正在使用的版本!重新运行mvn clean install希望这会有所帮助

答案 1 :(得分:1)

可悲的是,web.xml文件中有空格

示例:

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

之类的

答案 2 :(得分:0)

问题原因:

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

你可以在那个罐子里找到这个课:

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

如果你使用maven,你可以添加相应的依赖。

因此春季4不可能采用这种配置