有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java applicationContext是如何加载的?

网络的内容。xml

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/krams/*</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

WEB.INF中还有一个applicationContext.xml文件,但它在web.xml中不存在。怎么装的


共 (1) 个答案

  1. # 1 楼答案

    ContextLoaderListernerContextLoaderXmlWebApplicationContext类负责加载applicationContext。xml

    请看一下他们在git上的源代码

    您将发现ContextLoaderListerner正在扩展ContexLoader

    在{a2}文件中,他们提到:

     * If not explicitly specified, the context implementation is supposed to use a
     * default location (with XmlWebApplicationContext: "/WEB-INF/applicationContext.xml").
    

    这个值在XmlWebApplicationContext中提到如下(如果需要,还有其他常量值):

    /** Default config location for the root context */
    public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
    
    /** Default prefix for building a config location for a namespace */
    public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
    
    /** Default suffix for building a config location for a namespace */
    public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
    

    这就是为什么要使用applicationContext的原因。xml文件已加载,但未在web中配置。xml文件。您需要一些时间来理解Spring使用的所有这些默认配置(magic)