5 Spring 入门 web.xml配置详解

1.在WEB-INF的lib下面导入jar包

2.在web.xml里面配置Spring Mvc ,即,配置DispatcherServlet。(DispatcherServlet实际上就是一个servlet。

3. 启动Spring

4.spring-servlet.xml配置【

spring-servlet.xml配置

        spring-servlet这个名字是因为上面web.xml中标签配的值为spring(name>spring me>),再加上“-servlet”后缀而形成的spring-servlet.xml文件名,如果改为springMVC,对应的文件名则为springMVC-servlet.xml。

5.applicationContext文件命名为:-context.xml,如 root-context.xml;(如果有上下文的话)




Spring分为多个文件进行分别的配置,其中在servlet-name中如果没有指定init-param属性,那么系统自动寻找的spring配置文件为[servlet-name]-servlet.xml。
当需要载入多个spring相关的配置文件时,首先加载ContextLoaderListener类,再指定context-param中指定多个spring配置文件,使用逗号分别隔开各个文件。为了使用方便可以将配置文件进行MVC式的分解,配置控制器Bean的配置文件放置在一个xml文件中,server的Bean放在service.xml文件中。
指定的该servlet接管的url的行为,此处为了简便起见使用*.*,则表示在URL只要是在本机使用的任何request都是由该dispatchServlet来处理。


目前,spring提供了两种加载器,以供web容器的加载:一种是ContextLoaderListener,另一种是ContextLoaderServlet。这两种在功能上完全相同,只是一种是基于Servlet2.3版本中新引入的Listener接口实现,而另一种是基于Servlet接口实现,以下是这两种加载器在web.xml中的时机配置应用:

第一种:

 org.springframework.context.ContextLoaderListener



另一种:

 context
 org.springframework.context.ContextLoaderServlet
 1



通过上面的配置,web容器会自动加载applicationcontext.xml初始化。
如果需要指定配置文件的位置,可通过context-param加以指定:

 contextConfigLocation
 /WEB-INF/myApplicationContext.xml


之后,可以通过

WebApplicationContextUtils.getWebApplicationContext方法在web应用中获取applicationcontext的引用。





-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------                                                                                 x详解


 -----------------------------------------------------------------------------------------------------------------------------------

2.详解DispatcherServlet处理的请求必须在同一个web.xml文件里使用url-mapping定义映射。

   

A:配置

B:   

   test  
    org.springframework.web.servlet.DispatcherServlet  


     
        contextConfigLocation  
        classpath:source/servlet/test-servlet.xml  
   
 


    1  
 


 
    test  
    /*.do  
 


这样所有以.do结尾的请求都会被servlet test处理。

DispatcherServlet 的初始化过程中,Spring会在web应用的 WEB-INF 文件夹下寻找名为 [servlet-name]-servlet.xml 的配置文件,生成文件中定义的bean。这些bean会覆盖在全局范围(global cope)中定义的同名的bean。

如果servletName-servlet.xml不在默认路径下必须显示指定。【classpath:source/servlet/test-servlet.xml

   

在需要多个Spring xml文件的时候  
   
 
       
contextConfigLocation  
       
classpath:root-context.xml  
   
 

3.详解  
     
        
            org.springframework.web.context.ContextLoaderListener  
         
     

4     详解

存放位置:
1
src下面
需要在web.xml中定义如下:

  contextConfigLocation
  classpath:applicationContext.xml

2WEB-INF下面
需要在web.xml中定义如下:

 contextConfigLocation
 WEB-INF/applicationContext*.xml

 

web.xml通过contextConfigLocation配置spring的方式 
SSI
框架配置文件路径问题:

struts2 1+N 路径:src+src(可配置)     名称: struts.xml  + N 
spring
1          路径:src                         名称: applicationContext.xml 
ibatis
1+N 路径: src+src(可配置)    名称: SqlMapConfig.xml + N


部署到tomcat后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下

spring配置文件在启动时,加载的是web-info目录下的applicationContext.xml, 
运行时使用的是web-info/classes目录下的applicationContext.xml

配置web.xml使这2个路径一致:


 
  contextConfigLocation 
 /WEB-INF/classes/applicationContext.xml 

多个配置文件的加载 
 
        contextConfigLocation 
         
           classpath*:conf/spring/applicationContext_core*.xml, 
           classpath*:conf/spring/applicationContext_dict*.xml, 
           classpath*:conf/spring/applicationContext_hibernate.xml, 
           classpath*:conf/spring/applicationContext_staff*.xml, 
           classpath*:conf/spring/applicationContext_security.xml 
           classpath*:conf/spring/applicationContext_modules*.xml 
           classpath*:conf/spring/applicationContext_cti*.xml 
           classpath*:conf/spring/applicationContext_apm*.xml 
       
 
   

contextConfigLocation参数定义了要装入的 Spring配置文件。

 

首先与Spring相关的配置文件必须要以"applicationContext-"开头,要符合约定优于配置的思想,这样在效率上和出错率上都要好很多。 
还有最好把所有Spring配置文件都放在一个统一的目录下,如果项目大了还可以在该目录下分模块建目录。这样程序看起来不会很乱。 
web.xml中的配置如下: 
Xml
代码 
 
contextConfigLocation 
classpath*:**/applicationContext-*.xml  

"**/"表示的是任意目录; 
"**/applicationContext-*.xml"
表示任意目录下的以"applicationContext-"开头的XML文件。 
你自己可以根据需要修改。最好把所有Spring配置文件都放在一个统一的目录下,如:

 
 
  contextConfigLocation 
 classpath:/spring/applicationContext-*.xml 

 

web.xmlclasspath:classpath*:,有什么区别?

 

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



你可能感兴趣的:(2,SSM)