Spring无法自动装载Webapp上下文

时间:2013-09-16 13:17:16

标签: java spring spring-mvc

我的IDE(intelliJ)抱怨“无法自动发送没有找到WebApplicationContext类型的bean”,当我运行测试时,它在创建mockMvc时失败;

"ClassNotFoundException:org.springframework.web.context.request.async.CallableProcessingInterceptor

任何想法应该遗漏什么?

这是我的测试应用程序上下文文件的样子;

<context:annotation-config />
<mvc:annotation-driven />
<context:property-placeholder location="
        file:src/main/resources/conf/${user.name}/my.properties,
        file:src/main/resources/conf/${user.name}/my-junit.properties"/>

<context:component-scan base-package="my.main.package"/>

<import resource="classpath:META-INF/SomeOtherContext.xml"/>

这就是我的测试类的样子;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({
   "file:/src/test/resources/META-INF/MyTestContext.xml"
})

public class ServiceTest {

  @Autowired
  private WebApplicationContext context;

@Before
public void init() throws NoSuchFieldException {

    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();

}


}

POM文件是这样的(下面是父和子POM的组合来显示内容);

  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>


    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
    </dependency>

    <dependency>
        <groupId>org.jibx</groupId>
        <artifactId>jibx-run</artifactId>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <dependency>
        <groupId>com.sybase</groupId>
        <artifactId>jconnect</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>

    <dependency>
        <groupId>junit-addons</groupId>
        <artifactId>junit-addons</artifactId>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
    </dependency>

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


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

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>

父Pom是;

   <properties>
    <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
    <schema.dir>src/main/resources/xsd</schema.dir>
    <spring.version>3.1.1.RELEASE</spring.version>
    <spring.security.version>3.1.3.RELEASE</spring.security.version>
    <slf4j.version>1.6.4</slf4j.version>
    <jconnect.version>6.0</jconnect.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-context</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-beans</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-expression</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-web</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-jdbc</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-tx</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-asm</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-beans</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-context</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>


        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-jsp</artifactId>
            <version>2.2.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-nop</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>jcl-over-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-run</artifactId>
            <version>1.2.4.5</version>
            <exclusions>
                <exclusion>
                    <!-- xpp3 dependency includes a core javax.xml.namespace.QName class -->
                    <groupId>xpp3</groupId>
                    <artifactId>xpp3</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.12</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>com.sybase</groupId>
            <artifactId>jconnect</artifactId>
            <version>${jconnect.version}</version>
            <scope>provided</scope>
        </dependency>

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

        <dependency>
            <artifactId>jboss-logging</artifactId>
            <groupId>org.jboss.logging</groupId>
            <version>3.1.0.GA</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit-addons</groupId>
            <artifactId>junit-addons</artifactId>
            <version>1.4</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-mock</artifactId>
            <version>2.0.8</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.0.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

2 个答案:

答案 0 :(得分:3)

调试此操作的第一步:编写一个单元测试,将类作为资源加载,即:

 @Test
 public void testClassLoading() {
     Enumeration<URL> urls = getClass().getClassLoader().getResources("org/springframework/web/context/request/async/CallableProcessingInterceptor.class");
     int count = 0;
     while( urls.hasMoreElements() ) {
         System.out.println( urls.nextElement() );
         count ++;
     }
     assertEquals( "Expected this class exactly once", 1, count );
 }

我在单元测试中有几个这样做可以防止出现简单的类路径问题(重复或缺少依赖项)。

注意:这必须是独立的单元测试类。如果Spring无法初始化,则可能无法运行任何测试方法。

现在你发现测试返回0表示类不在类路径上。

然后第二步找到哪个JAR包含此类:转到http://search.maven.org/#advancedsearch%7Cgav并在正确的字段中输入类名。

搜索会产生此类位于org.springframework:spring-web:jar:3.2.x.RELEASE而不是3.1。

结论:您的类路径有问题。你可能会混合Spring版本。不要那样做。

答案 1 :(得分:2)

父POM添加后的第二个答案

您的父POM正在引入多个版本的Spring Framework。具体做法是:

父POM:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-mock</artifactId>
    <version>2.0.8</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>commons-logging</artifactId>
            <groupId>commons-logging</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>3.2.0.RELEASE</version>
    <scope>test</scope>
</dependency>

Project POM

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

您无法混合框架库版本。

原始答案

Aaron的回答对调试非常有用。 M. Deinum上面的评论也可能是相当的 相关的,因为我不再确定3.1中包含缺失的类。在3.2发布之前我找不到它。由于在调试时弄乱了版本,你可能会遇到冲突的jar。

也许尝试'mvn clean'然后'mvn依赖:copy-dependencies or mvn package`。然后,您可以查看maven认为您需要的jar文件的完整列表。特别是,检查Spring库的不匹配或重复版本。

您提到使用IDE作为问题的开头。也许您的IDE未配置为在运行测试时在其类路径中使用完整的maven依赖关系列表。