zoukankan      html  css  js  c++  java
  • spring配置文件默认名称及位置,ContextLoaderListener监听器作用

    当在新建的maven web项目的web.xml中加入了ContextLoaderListener的监听后,直接运行程序就会这样,提示找不到spring的配置文件,且默认位置为/WEB-INF/applicationContext.xml,即默认名称为applicationContext.xml

    spring在web.xml中的配置

    由于spring需要启动容器才能为其他框架提供服务,而web应用程序的入口是由web服务器控制的,因此无法在main()方法中通过创建ClassPathXmlApplicationContext对象来启动spring容器。spring提供了org.springframework.web.context.ContextLoaderListener(spring-web依赖下)这个监听器类来解决这个问题。该监听器实现了ServletContextListener接口,可以在web容器启动的时候初始化spring容器。当然,前提是需要在web.xml中配置好这个监听器。如下

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

    如果没有指定contextConfigLoaction参数,ContextLoaderListener默认会去查找/WEB-INF/applicationContext.xml。换句话说,如果我们将spring的配置文件命名为applicationContext.xml并存放在WEB-INF目录下,即使不指定contextConfigLocation参数,也能加载配置文件。否则就需要配置如下

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-jdbc.xml</param-value>
    </context-param>
  • 相关阅读:
    redis之通信开销限制redis cluster规模的关键因素
    redis解决秒杀问题与数据倾斜
    codis与redis
    脑裂中的数据丢失
    redis主从同步与故障切换的配置项可能出现的问题
    redi事务机制
    redis并发访问的问题之实现分布式锁
    redis的小功能 bitmaps 发布订阅,HyperLogLog GEO LUA
    redis之api学习
    Ubuntu17.04安装WineQQ7.8及微信
  • 原文地址:https://www.cnblogs.com/yanguobin/p/11665094.html
Copyright © 2011-2022 走看看