spring那些事 研究IOC容器2

web环境下的ApplicationContext和无web环境的ApplicationContext的不同之处

web中的ApplicationContext从何处获得?

配置在web.xml中的ContextLoaderListener 继承自org.springframework.web.context.ContextLoader 这个类中定义了默认的AppcationContext类org.springframework.web.context.support.XmlWebApplicationContext

private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";

在ContextLoader.properties放置了默认的web环境下的ApplicationContext,那么如果我么修改默认的ApplicationContext会发生什么呢?
我们在这里通过更改web.xml中的配置参数达到更改ApplicationContext类的目的。

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.context.support.ClassPathXmlApplicationContext</param-value>
</context-param>

毫不犹豫的报错:
Custom context class [org.springframework.context.support.ClassPathXmlApplicationContext] is not of type [org.springframework.web.context.ConfigurableWebApplicationContext]
在这里通过一个Adapter实现ClassPathXmlApplicationContext的适配

public class ClassPathXmlApplicationContextAdapter extends ClassPathXmlApplicationContext implements ConfigurableWebApplicationContext  

同时实现其中的方法,之后再运行我们就把ClassPathXmlApplicationContext 启动了,看来二者的关键在于ConfigurableWebApplicationContext的区别,通过这个接口spring 集成了web环境,但在这个过程中遇到一个问题:始终读取不到正确的配置文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值