zoukankan      html  css  js  c++  java
  • spring web.xml 标签<param-name>contextConfigLocation</param-name>

        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:/account-persist.xml
                classpath:/account-captcha.xml
                classpath:/account-email.xml
                classpath:/account-service.xml
            </param-value>
        </context-param>

    这个是maven实战12章中的项目配置

    • 初始化过程:
      1. 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>。
      2. 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。
      3. 接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。
      4. 容器创建<listener></listener>中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。
      5. 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。
      6. 得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。

       

    由上面的初始化过程可知容器对于web.xml的加载过程是context-param >> listener  >> fileter  >> servlet

    •  如何使用
    1. 页面中

      ${initParam.contextConfigLocation}

    2. Servlet中
      String paramValue=getServletContext().getInitParameter("contextConfigLocation")

        声明:此内容为抄袭所得

  • 相关阅读:
    VIM 第二天--快捷键
    Vim 第一天
    The Salt Master has cached the public key报错解决办法
    nginx启用TCP反向代理日志配置
    FastDFS数据迁移
    nginx修改上传文件大小限制
    ipv6禁用导致rpcbind服务启动失败解决办法
    ELK之elasticdump迁移es数据
    MySQL+Amoeba实现数据库读写分离
    使用Zabbix监控RabbitMQ消息队列
  • 原文地址:https://www.cnblogs.com/zytcomeon/p/14140722.html
Copyright © 2011-2022 走看看