zoukankan      html  css  js  c++  java
  • Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

    Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

    转自http://www.cnblogs.com/cczz_11/p/4363314.html 

    https://blog.csdn.net/u013743160/article/details/52734017

    一、首先写一下代码结构。





    二、再看web.xml中的配置情况。
    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
    3.   <display-name>SpringMVC</display-name>  
    4.   <welcome-file-list>  
    5.     <welcome-file>index.html</welcome-file>  
    6.     <welcome-file>index.htm</welcome-file>  
    7.     <welcome-file>index.jsp</welcome-file>  
    8.     <welcome-file>default.html</welcome-file>  
    9.     <welcome-file>default.htm</welcome-file>  
    10.     <welcome-file>default.jsp</welcome-file>  
    11.   </welcome-file-list>  
    12.   <context-param>  
    13.             <param-name>contextConfigLocation</param-name>  
    14.             <!-- <param-value>classpath*:config/applicationContext.xml</param-value> -->  
    15.             <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>  
    16.   </context-param>  
    17.     
    18.   <listener>  
    19.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    20.   </listener>  
    21.      <servlet>   
    22.         <servlet-name>springmvc</servlet-name>   
    23.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    24.         <init-param>  
    25.            <param-name>contextConfigLocation</param-name>  
    26.            <!-- <param-value>classpath*:config/Springmvc-servlet.xml</param-value> -->  
    27.            <param-value>/WEB-INF/classes/config/Springmvc-servlet.xml</param-value>  
    28.         </init-param>  
    29.         <load-on-startup>1</load-on-startup>   
    30.     </servlet>    
    31.     <servlet-mapping>   
    32.         <servlet-name>springmvc</servlet-name>   
    33.         <url-pattern>/</url-pattern>    
    34.     </servlet-mapping>  
    35. </web-app>  
     


    三、下面是对配置文件的说明。

    [html] view plain copy
     
    1. <listener>  
    2.   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    3. </listener>  


    ContextLoaderListener是Spring的监听器,它的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

    [html] view plain copy
     
    1. <context-param>  
    2.           <param-name>contextConfigLocation</param-name>  
    3.           <!-- <param-value>classpath*:config/applicationContext.xml</param-value> -->  
    4.           <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>  
    5. </context-param>  


    这段配置是用于指定applicationContext.xml配置文件的位置,可通过context-param加以指定:


    这里需要搞清楚classpath是什么,以及classpath:和classpath*有何区别:


    1. 首先 classpath是指 WEB-INF文件夹下的classes目录


    2. classpath 和 classpath* 区别: 
    classpath:只会到你的class路径中查找找文件; 
    classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找. 


     


    如果applicationContext.xml配置文件存放在src目录下,就好比上面的代码结构中的存放位置,那么在web.xml中的配置就如下所示:

    [html] view plain copy
     
    1. <context-param>  
    2.     <param-name>contextConfigLocation</param-name>  
    3.     <param-value>classpath:applicationContext.xml</param-value>  
    4. </context-param>  


    如果applicationContext.xml配置文件存放在WEB-INF下面,那么在web.xml中的配置就如下所示:

    [html] view plain copy
     
    1. <context-param>  
    2.     <param-name>contextConfigLocation</param-name>  
    3.     <param-value>WEB-INF/applicationContext*.xml</param-value>  
    4. </context-param>  


    需要注意的是,部署到应用服务器后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下,spring的 配置文件在启动时,加载的是web-info目录下的applicationContext.xml, 运行时使用的是web-info/classes目录下的applicationContext.xml。因此,不管applicationContext.xml配置文件存放在src目录下,还是存放在WEB-INF下面,都可以用下面这种方式来配置路径:

    [html] view plain copy
     
    1. <context-param>  
    2.     <param-name>contextConfigLocation</param-name>  
    3.     <param-value>WEB-INF/applicationContext*.xml</param-value>  
    4. </context-param>  


    当有多个配置文件加载时,可采用下面代码来配置:


    复制代码
    [html] view plain copy
     
    1. <context-param>  
    2.     <param-name>contextConfigLocation</param-name>  
    3.     <param-value>   
    4.         classpath*:conf/spring/applicationContext_core*.xml,   
    5.         classpath*:conf/spring/applicationContext_dict*.xml,  
    6.         classpath*:conf/spring/applicationContext_hibernate.xml,  
    7.         ......  
    8.     </param-value>  
    9. </context-param>  


    复制代码
    也可以用下面的这种方式:
    [html] view plain copy
     
    1. <context-param>  
    2.     <param-name>contextConfigLocation</param-name>  
    3.     <param-value>classpath*:**/applicationContext-*.xml</param-value>  
    4. </context-param>  


    "**/"表示的是任意目录; 


    "**/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。 


    Spring配置文件最好以"applicationContext-"开头,且最好把所有Spring配置文件都放在一个统一的目录下,也可以分模块创建。
    文章标签: spring
     
    在web.xml中如何加载aplicationContext.xml文件

    web.xml的写法

    [html] view plain copy
     
    1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    4.     version="2.5">  
    5.   
    6.     <display-name>demo-web</display-name>  
    7.       
    8.     <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔  
    9.         此参数用于后面的Spring Context Loader -->  
    10.     <context-param>  
    11.         <param-name>contextConfigLocation</param-name>  
    12.         <param-value>  
    13.             classpath*:/applicationContext.xml  
    14.             classpath*:/applicationContext-shiro.xml  
    15.         </param-value>  
    16.     </context-param>  
    17.   
    18.     <!--Spring的ApplicationContext 载入 -->  
    19.     <listener>  
    20.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    21.     </listener>  
    22.   
    23. </web-app></span
    applicationContext.xml的两种加载方式

    第一种:直接将之放到/WEB-INF下,在web.xml中声明一个listener;

    <listener>
     <listener-class>                     org.springframework.web.context.ContextLoaderListener
     </listener-class>
    </listener>
    • 1
    • 2
    • 3
    • 4
              默认的路径是/WEB-INF/applicationContext.xml
    
    • 1
    • 2

    第二种:将之放到classpath下,但是此时要在web.xml中加入,用它来指明你的applicationContext.xml的位置以供web容器来加载。按照Struts2 整合spring的官方给出的档案,写成:

    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>
        /WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
        </param-value> 
    </context-param>
    抱怨没有用,只能靠自己
  • 相关阅读:
    springboot2.1.3+jacoco检测代码覆盖率
    springboot2.1.3使用mvn site遇到的坑及解决方案
    docker安装mysql5.6(win10家庭版)
    Windows10家庭版安装docker攻略
    python 实例方法,类方法,静态方法的调用
    python超继承
    python 测试生成Htmltestrunner报告几个参数
    Python3 中类的静态方法、普通方法、类方法
    python 操作文件
    1
  • 原文地址:https://www.cnblogs.com/mybatis/p/9088652.html
Copyright © 2011-2022 走看看