指定spring配置文件的位置 ContextLoaderListener DispatcherServlet

  • 默认MVC配置文件

在web.xml文件中配置:

<!-- front controller -->  
<servlet>  
    <servlet-name>annomvc</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      
</servlet>  
<servlet-mapping>  
    <servlet-name>annomvc</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>


指定Spring来处理请求的Servlet,默认查找mvc配置文件的地址是:/WEB-INF/${servletname}-servlet.xml,示例中默认查找的mvc配置文件是:/WEB-INF/annomvc-servlet.xml。

<!-- front controller -->  
<servlet>  
    <servlet-name>annomvc</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:config/annomvc-servlet.xml</param-value>  
    </init-param>  
</servlet>  
<servlet-mapping>  
    <servlet-name>annomvc</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping> 


要修改mvc配置文件的位置,需要在配置DispatcherServlet时指定mvc配置文件的位置,只需要在配置DispatcherServlet时指定<init-param>标签。

  • 其他配置文件

这里的其他配置文件,指的是对datasource的配置、persistence层的配置、service层的配置信息等。要加载其他配置文件,需要在web.xml配置文件中加入一个ContextLoaderListener监听器来配置。ContextLoaderListener只监听初始化除mvc相关配置之外的bean。

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

若没有指定其他参数,默认查找的配置文件位置是:/WEB-INF/applicationContext.xml。

<!-- context load listener -->  
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  
<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>  
        classpath:config/service-context.xml  
        classpath:config/persistence-context.xml  
        classpath:config/datasource-context.xml  
        </param-value>  
</context-param>

 

要修改除mvc配置文件这之外的其他bean的配置文件位置,只需要在web.xml中加入<context-param>标签。
 

  • ContextLoaderListener&ServletDispatcher

Spring中有两种上下文环境“Application Context”和“Web Application Context”,它们分别对应ContextLoaderListener和ServletDispatcher,且者可以用来配置bean的注入、装配、AOP。

 

1. ContextLoaderListener

 

ContextLoaderListener通过读取contextConfigLocatiion参数来读取配置参数,一般来说它配置的是Spring项目的中间层。对应到Spring的自动装配机制<context:component-scan>就是以下几种注解的装配。

(1) DAO : such as @Repository bean

(2) Entity : such as @Entity bean

(3)Service: such as @Service bean

 

2. ServletDispatcher

它配置的是Web层组件的注入、装配和AOP。

(1)Controller

(2)ViewResolvers

(3)LocaleResolvers

(4)ThemeResolvers

 

最后欢迎大家访问我的个人网站:1024s

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值