这特么是啥系列之----webx学习笔记

<listener>

 <listener-class>com.test.citrus.webx.context.WebxContextLoaderListener</listener-class>

</listener>

该监听器实现了spring的ContextLoaderListener来负责在启动的时候加载Webx环境的


public class WebxContextLoaderListener extends ContextLoaderListener

public class ContextLoaderListener implements ServletContextListener

public interface ServletContextListener extends EventListener {

public void contextInitialized ( ServletContextEvent sce );

public void contextDestroyed ( ServletContextEvent sce );

}

这两个方法一个是容器初始化完成的时候需要做的事情

一个是容器销毁的时候做的事情

public class WebxContextLoaderListener extends ContextLoaderListener {
    @Override
    protected final ContextLoader createContextLoader() {
        return new WebxComponentsLoader() {

            @Override
            protected Class<? extends WebxComponentsContext> getDefaultContextClass() {
                Class<? extends WebxComponentsContext> defaultContextClass = WebxContextLoaderListener.this
                        .getDefaultContextClass();

                if (defaultContextClass == null) {
                    defaultContextClass = super.getDefaultContextClass();
                }

                return defaultContextClass;
            }
        };
    }

    protected Class<? extends WebxComponentsContext> getDefaultContextClass() {
        return null;
    }
}

以上是webxcontextloaderlistener的源码

  • createContextLoader返回的是一个ContextLoader类的实例。
  • 方法里new出来一个WebxComponentsLoader的实例作为返回值,因此,这个返回值实际上是WebxComponentsLoader类的实例。看这个名字,Webx组件加载器,就知道,这货只是个加载器,类似于PC的引导程序。
  • 方法把这个实例new出来的时候,顺便覆盖了一下WebxComponentsLoader类的getDefaultContextClass方法,因此,返回的实例实际上是一个WebxComponentsLoader类的匿名子类的实例,且这个子类覆盖了getDefaultContextClass方法。

BeanFactory

public interface BeanFactory {
    ...
    Object getBean(String name) throws BeansException;

    Object getBean(String name, Class requiredType) throws BeansException;

    Object getBean(String name, Object[] args) throws BeansException;

    boolean containsBean(String name);

    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

    boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException;

    Class getType(String name) throws NoSuchBeanDefinitionException;

    String[] getAliases(String name);
}

可见,BeanFactory极其简洁的定义了与Bean有关的一些功能,在需要的时候,你可以直接向其索取。

ApplicationContext是BeanFactory的子接口,提供了一些扩展的功能。

WebApplicationContext是ApplicationContext的子接口,提供了一些与Web容器相关的功能。


断点总结

总结一下

在web.xml中配置WebxContextLoaderListener

要说的是这个listener继承了ContextLoader,并且实现了ServletContextListener

ContextLoader中有

private WebApplicationContext context;
ServletContextListener中实现了EventListener,所以这个监听器WebxContextLoaderListener需要实现里面的contextInitialized方法
 

初始化这个监听器会调用这个contextInitialized这个方法,以下是这个方法源码

public void contextInitialized(ServletContextEvent event) {
   initWebApplicationContext(event.getServletContext());
}

没错,就这么点,调用了父类ContextLoader中的init方法初始化容器(webx重写了这个方法,调用了自己的init()方法来做一些...不为人知的事情)

当然,初始化Listener的时候是要先调用ContextLoader的静态方法(父类的静态代码块之类的),加载一些默认的策略值

默认策略的值放在ContextLoader.properties(这里面有指定webApplicationCaontext的实例化对象为...webx也对其做了更改)

ContextLoader初始化过程中,会通过xml中contextConfigLocation的配置,获取Spring相关的xml文件所在路径,所以一般在web.xml中,还需要配置如下配置(举例,实际value值根据项目的配置进行填写)

那,这个initWebApplicationContext(event.getServletContext())都干了些什么呢...

1.确定在初始化上下文中只存在一个ContextLoader* 相关的配置,否则抛出异常

2.打印相关日志,并且计算开始时间。

3.在本地实例变量中存储上下文,保证它在ServletContext关闭时可用。其中createWebApplicationContext方法的作用是 “实例化该加载器的根WebApplicationContext,如果指定了默认上下文类或自定义上下文类”。默认上下文即 xmlWebApplicationContext。 

4.判断应用上下文是否是属于ConfigurableWebApplicationContext实例。默认的xmlWebApplicationContext是属于ConfigurableWebApplicationContext实例的。如果应用上下文没有生效,需要确认应用上下文是否设置了父上下文。loadParentContext方法主要功能是“具有默认实现(可能被子类覆盖)的模板方法,以加载或获取将用作根WebApplicationContext的父上下文的ApplicationContext实例。” configureAndRefreshWebApplicationContext的主要作用就是“获取web.xml中配置的contextConfigLocation路径,获取应用的访问路径,获取spring相关的XML配置文件的路径,并且刷新当前的WebApplicationContext配置相关。” 最后将该上下文对象放入servlet上下文参数中。 

5.获取当前线程的类加载器, 如果ContextLoader的类加载器和当前线程的类加载器一样,则应用上下文对象赋值给currentContext。 


至此为止,contextLoaderListener的contextInitialized方法执行完成。在这个方法里面我们初始化了ServletContext的实例对象,并且获得了spring配置相关的xml文件。


嗯..不一定理解正确..自己能看懂就行,等明天接着看..














评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值