Spring 无法加载/WEB-INF/applicationContext.xml

Spring 无法加载/WEB-INF/applicationContext.xml,spring,Spring,在其他查询中看到相同的问题,但没有找到答案,因此重新发布 在Rest中创建了一个示例Helloworld服务,并将其部署在Tomcat Jersey中。不使用Maven。 在WEB-INF目录中的applicationContext.xml中注入Springbean定义 在Eclipse中的Tomcat中部署了该服务。 但是当我执行rest服务时,找不到文件applicationContext.xml。 一旦我仅将文件复制到WEB-INF/classes,它就可以工作了 只有将其保存在WEB-I

在其他查询中看到相同的问题,但没有找到答案,因此重新发布

在Rest中创建了一个示例Helloworld服务,并将其部署在Tomcat Jersey中。不使用Maven。 在WEB-INF目录中的applicationContext.xml中注入Springbean定义

在Eclipse中的Tomcat中部署了该服务。 但是当我执行rest服务时,找不到文件applicationContext.xml。 一旦我仅将文件复制到WEB-INF/classes,它就可以工作了

只有将其保存在WEB-INF中才能使其正常工作的方法是什么

web.xml:

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

将上下文描述符放在
/WEB-INF/classes
下似乎是合理的,因为该位置是
类路径的根

因此,如果要将applicationContext.xml放到WEB-INF文件夹下,请尝试加载以下uri(不带前缀):

否则,您能解释一下为什么要在web应用程序中加载applicationContext.xml吗

或者,您可以按如下方式以编程方式加载应用程序上下文:

ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

BR.

如果您正在使用
Intellj IDEA
并且在
ApplicationContext.xml
路径方面存在问题; 转到
文件
并单击
项目结构
。在
项目设置
下,单击
方面
,在
网站
部分,您将看到
网站资源目录
。您应该将
Web资源目录
定义到
/Web-INF
文件夹中。如果您的
/WEB-INF
文件夹位于下面的子路径中

/web/web-INF/


然后选择
/web
目录覆盖您的所有文件。

您只是想从代码访问您的上下文吗?您没有实现ContextAware的任何原因?这取决于您的contextConfigLocation,如果WEB INF中存在文件,则应加载该位置。感谢您的响应。但上面给出代码的类只是一个资源类,而不是servlet。因为这是一个简单的helloworld,所以我将SpringServlet称为要加载到web.xml.xml中的servlet。尝试了新的类路径XmlApplicationContext(“/web-INF/applicationContext.xml”),但这会导致FileNotFound。也许它正在查找类路径的WEB-INF/classesRoot下的/WEB-INF/applicationContext.xml是/WEB-INF还是/WEB-INF/classes?由于web.xml位于/web-INF中并且是起始点,对吗?
classpath
引用了
/web-INF/classes
,这就是为什么当您将applicationCOntext.xml移到那里时可以成功加载它的原因。只是一个资源类,而不是servlet(如果它位于同一个webapp中),因此无论它是否为servlet,它都可以访问主servlet上下文,并且ServletCOntext可以被注入到您的ResourceRece中。但是我不能给出更多的提示,除非你给出一些你所谓的
资源
:)
    ctx = new ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml");
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());