zoukankan      html  css  js  c++  java
  • web.xml中,spring模块化加载xml方式

    1:web.xml中添加监听器。

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

    监听器作用为spring上下文在web容器初始化的时候加载spring相关配置。

    查看源码可以看到依赖关系:

    public class ContextLoaderListener extends ContextLoader implements ServletContextListener
    父类ContextLoader中,有如下全局变量参数,为监听器获取初始化对象的引用值:
        /**
         * Name of servlet context parameter (i.e., {@value}) that can specify the
         * config location for the root context, falling back to the implementation's
         * default otherwise.
         * @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION
         */
        public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

    注释中描述了这是指定了根上下文的位置,CONFIG_LOCATION_PARAM是上下文参数名称,如果没有,那默认位置为:XmlWebApplicationContext,我们详细看下这个类,DEFAULT_CONFIG_LOCATION默认/WEB-INF/applicationContext.xml这个文件为spring的context配置文件。其余参数指定文件夹位置,后缀名称。

    public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {
    
        /** 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";

    所以,web.xml中我们可以添加如下配置:

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath*:/config/spring/spring*.xml
            </param-value>
        </context-param> 

    这样即实现了,config/spring/目录下,所有以spring字符串开头的xml配置文件。

    效果图:

  • 相关阅读:
    《Graph Attention Network》阅读笔记
    Spark 中的机器学习库及示例
    Spark 中 RDD的运行机制
    Spark 的 python 编程环境
    1.10系统调用和库函数
    nginx中的configure脚本
    nginx中的main函数
    IPv4,IPv6套接字地址结构
    unix网络编程中的地址转换函数
    有关nginx中Strings模块中ngx_explicit_memzero()函数的死区消除优化问题
  • 原文地址:https://www.cnblogs.com/somewhater/p/8706919.html
Copyright © 2011-2022 走看看