Spring容器初始化

一,容器初始化
1,在web.xml中配置ContextLoaderListener
    这个监听器实现了ServletContextListener,这个接口有2个方法。
    public void contextInitialized ( ServletContextEvent sce );
    public void contextDestroyed ( ServletContextEvent sce );

    参数ServletContextEvent能直接获取servletContext也就是java web容器的上下文。
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>

    从而也就能获取到context-config的数据。

    监听器的父类ContextLoader首先执行静态代码块。
    (复习下类的初始化顺序:
        父类静态代码块,实例变量
        子类静态代码块,实例变量
        父类非静态代码块,实例变量
        子类非静态代码块,实例变量
        父类构造方法
        子类构造方法
    )

    static {
        // Load default strategy implementations from properties file.
        // This is currently strictly internal and not meant to be customized
        // by application developers.
        try {
             //DEFAULT_STRATEGIES_PATH = ContextLoader.properties;
            //class path resource [org/springframework/web/context/ContextLoader.properties]
            
            //在ContextLoader.properties中只有一对键值对:
            //org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
            ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
            //将ContextLoader.properties中的值,复制到defaultStrategies这个Properties对象中。(默认策略)
            defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
        }
        catch (IOException ex) {
            throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
        }
    }

    我认为 ContextLoaderListener调用的是无参构造方法.
    然后是执行contextInitialized 方法,调用父类的 initWebApplicationContext方法,将ServletContext作为参数传递.
    那么, initWebApplicationContext(ServletContext)这个方法做了些什么呢?

         /**
     * Initialize Spring's web application context for the given servlet context, 使用servletContext初始化spring web application context 
     * using the application context provided at construction time, or creating a new one 
     * according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and
     * "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params.
     * @param servletContext current servlet context
     * @return the new WebApplicationContext
     * @see #ContextLoader(WebApplicationContext)
     * @see #CONTEXT_CLASS_PARAM
     * @see #CONFIG_LOCATION_PARAM
     */
    public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
        //检测servletContext的属性中是否存在spring的根上下文属性
        //如果存在则是错误的,因为表明已经有一个根上下文已经启动,再启动会冲突,所以避免重复启动!
        //不可以在web.xml中配置2个及多个ContextLoaderListener.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值