tomcat与springmvc 结合 之---第18篇 StandContext容器和SpringMVC的监听器的模型

writedby张艳涛

如何使用tomcat的监听器

public class BootStrap_ex06 {
    public static void main(String[] args) {
        HttpConnector connector = new HttpConnector();
        SimpleWrapper wrapper1 = new SimpleWrapper();
        wrapper1.setName("Primitive");
        wrapper1.setServletClass("PrimitiveServlet");
        SimpleWrapper wrapper2 = new SimpleWrapper();
        wrapper2.setName("Modern");
        wrapper2.setServletClass("ModernServlet");

        SimpleContext context = new SimpleContext();
        context.addChild(wrapper1);
        context.addChild(wrapper2);

        SimpleContextMapper mapper = new SimpleContextMapper();
        mapper.setProtocol("http");
        SimpleContextLifecycleListener listener = new SimpleContextLifecycleListener();

        ((Lifecycle) context).addLifecycleListener(listener);
        context.addMapper(mapper);
        SimpleLoader loader = new SimpleLoader();

        context.setLoader(loader);

        context.addServletMapping("/Primitive", "Primitive");
        context.addServletMapping("/Modern", "Modern");
        connector.setContainer(context);

        try {
            connector.initialize();
            ((Lifecycle) connector).start();
            ((Lifecycle) context).start();
            System.in.read();
            ((Lifecycle) context).stop();

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

可以看到将一个listener传给context.的lifecyclesupport对象

那么来看下spring的监听器,起点就是

FrameworkServlet

==>

createWebApplicationContext

==>

configureAndRefreshWebApplicationContext(wac);
==>

看到了,spring的监听器实现很复杂

spring的源码读起来比tomcat要难呢

posted @ 2021-07-20 19:23  张艳涛&java  阅读(59)  评论(0编辑  收藏  举报