spring 移动并重命名 IntelliJ IDEA 的默认 application-config.xml 后出现“未为此文件配置应用程序上下文”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16257074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Application context not configured for this file' error after moving and renaming the default application-config.xml of IntelliJ IDEA
提问by skiabox
I have created a Spring Mvc application using IntelliJ IDEA and then I moved and renamed the default application-config file to another directory. Now I am getting this error : 'Application context not configured for this file' The new place of the file is src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
我使用 IntelliJ IDEA 创建了一个 Spring Mvc 应用程序,然后我将默认的 application-config 文件移动并重命名到另一个目录。现在我收到此错误:“未为此文件配置应用程序上下文”文件的新位置是 src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
The file is this one:
该文件是这样的:
<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>
Any ideas? Thank you.
有任何想法吗?谢谢你。
回答by skiabox
I've configured application context from code (a new feature of spring 3.1) so I believe that IntelliJ idea will keep complaining. Here is the code.
我已经从代码中配置了应用程序上下文(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("/");
}
}
回答by Satur6ay
Check the config of spring in the web.xml file.
检查 web.xml 文件中 spring 的配置。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
The contextConfigLocation parameter config the xml location about spring, Check you web.xml is correct.
contextConfigLocation 参数配置spring 的xml 位置,检查web.xml 是否正确。
If you load the xml by java code,like @skiabox, you can ignore this warning.
如果你通过java代码加载xml,比如@skiabox,你可以忽略这个警告。
回答by Андрей Костров
Satur6ay's comment helps me particularry.
Satur6ay 的评论对我很有帮助。
But xml-file was coloured "red" by Idea. I found thar resources folder had not "resource"-icon, but had standard gray folder icon. So, I went to File -> Project Structure -> my module -> found there "resorces" folder -> "Mark as" -> Resources.
但是 xml 文件被 Idea 着色为“红色”。我发现资源文件夹没有“资源”图标,但有标准的灰色文件夹图标。因此,我转到文件 -> 项目结构 -> 我的模块 -> 找到“资源”文件夹 ->“标记为” -> 资源。
xml-reference in web.xml become valid and all other references in xml-spring-configs ()become green-valid
web.xml 中的 xml-reference 变为有效,而 xml-spring-configs () 中的所有其他引用变为绿色有效

