移动并重命名IntelliJ IDEA的默认application-config.xml后,“未为此文件配置应用程序上下文”错误

时间:2013-04-27 21:33:20

标签: spring spring-mvc intellij-idea

我使用IntelliJ IDEA创建了一个Spring Mvc应用程序,然后移动并将默认的application-config文件重命名为另一个目录。 现在我收到此错误:'没有为此文件配置应用程序上下文' 文件的新位置是src / main / webapp / WEB-INF / spring / appServlet / servlet-context.xml

文件就是这个:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven/>

    <mvc:resources mapping="/resources/**" location="/"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jspx"/>
    </bean>

    <context:component-scan base-package="com.apress.prospring3.ch17.web.controller"/>

</beans>

有什么想法吗? 谢谢。

4 个答案:

答案 0 :(得分:0)

我已经从代码中配置了应用程序上下文(Spring 3.1的一个新功能),所以我相信IntelliJ的想法会继续抱怨。 这是代码。

package com.apress.prospring3.ch17.web.init;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;


public class MyWebAppInitializer implements WebApplicationInitializer{

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        XmlWebApplicationContext appContext = new XmlWebApplicationContext();

        appContext.setConfigLocation("/WEB-INF/spring/appServlet/servlet-context.xml");

        ServletRegistration.Dynamic dispatcher = container.addServlet("appServlet", new DispatcherServlet(appContext));

        MultipartConfigElement multipartConfigElement = new MultipartConfigElement(null, 5000000, 5000000, 0);
        dispatcher.setMultipartConfig(multipartConfigElement);

        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

    }
}

答案 1 :(得分:0)

检查web.xml文件中的spring配置。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
    </param-value>
</context-param>

contextConfigLocation参数配置关于spring的xml位置, 检查web.xml是否正确。

如果您通过java代码加载xml,例如@skiabox,则可以忽略此警告。

答案 2 :(得分:0)

Satur6ay的评论对我很有帮助。

但是xml文件被Idea涂成红色。 我发现资源文件夹没有“ resource”图标,但是有标准的灰色文件夹图标。 因此,我去了File-> Project Structure->我的模块->在其中找到“ resorces”文件夹->“标记为”->资源。

web.xml中的

xml-reference变为有效,而xml-spring-configs()中的所有其他引用变为绿色有效

答案 3 :(得分:0)

添加这个对我有用!!感谢 satur6ay

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
    </param-value>
</context-param>