ContextLoaderListener与RequestContextListener的对比

ContextLoaderListener与RequestContextListener

(1)ContextLoaderListener

ContextLoaderListenerextends ContextLoader implements ServletContextListener

ServletContextListenerextends EventListener
ServletContextListener只负责监听Web容器的启动和关闭的事件。

ContextLoaderListener(或ContextLoaderServlet)将Web容器与spring容器进行整合。

这是使用Spring 必须配置 的:

<listener>

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

</listener>

Spring配置文件的声明:

<context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:applicationContext.xml</param-value>

</context-param>

如果没有显式声明,则 系统默认 在WEB-INF/applicationContext.xml。

在一个团队使用Spring的实际项目中,应该需要多个Spring的配置文件,如何使用和交叉引用的问题: 

如果想装入多个配置文件,可以用逗号作分隔符,如:


多个配置文件里的交叉引用可以用ref的external或bean解决,例如:

applicationContext.xml


dbBean在applicationContext-database.xml中

(2)RequestContextListener

RequestContextListener implementsServletRequestListener

ServletRequestListener extendsEventListener 
ServletRequestListener监听HTTP请求事件,Web服务器接收的每次请求都会通知该监听器。

RequestContextListener将Spring容器与Web容器结合的更加密切。这是可选配置,并且后者与scope=”request”搭配使用:

<listener>

 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

</listener>

两者区别

ContextLoaderListener(或ContextLoaderServlet)将Web容器与spring容器整合。RequestContextListener将Spring容器与Web容器结合的更加密切。 
前者为 必选配置,后者为 可选配置,并且后者与 scope=”request”搭配使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值