首页 > 解决方案 > ContextLoaderListener 触发器 applicationContext.xml 文件未找到

问题描述

我没有 applicationContext.xml。我不需要它。

这是我的web.xml文件:

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigurationLocation</param-name>
    <param-value>/WEB-INF/config/security-config.xml</param-value>
  </context-param>
  ...
<servlet>
    <servlet-name>fitTrackerServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servlet-config.xml</param-value>
    </init-param>
</servlet>

我在security-config.xml那里有那个文件,它看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.2.xsd 
        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-3.2.xsd">

    <http auto-config="true">
        <intercept-url pattern="/**" access="ROLE_USER"/>
    </http>
    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="ajde" password="ajde" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

而且我仍然得到错误,就像我没有一样applicationContext.xml

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

这怎么可能?!?

我看不出我做错了什么。

很明显,这是一条路径。但是将 servlet 配置 servlet-config.xml 放在相同的目录下工作。

我将我的复制/粘贴security-config.xml到 的根目录WEB-INF,然后将其重命名为applicationContext.xml...它可以工作!!!!!!

所以,我做错了什么?!?!?我看不到...

在此处输入图像描述

标签: javaspringweb.xml

解决方案


来自https://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-framework-reference/html/beans.html#context-create

侦听器检查 contextConfigLocation 参数。如果参数不存在,则监听器使用 /WEB-INF/applicationContext.xml 作为默认值。当参数确实存在时,侦听器使用预定义的分隔符(逗号、分号和空格)分隔字符串,并将这些值用作将搜索应用程序上下文的位置。也支持 Ant 样式的路径模式。示例是 /WEB-INF/*Context.xml 用于名称以“Context.xml”结尾的所有文件,位于“WEB-INF”目录中,/WEB-INF/**/*Context.xml 用于所有此类文件位于“WEB-INF”的任何子目录中。

您可以避免 applicationContext.xml,但它看起来像解决方法https://www.nurkiewicz.com/2011/01/spring-framework-without-xml-at-all.html


推荐阅读