zoukankan      html  css  js  c++  java
  • tomcat及springboot实现Filter、Servlet、Listener

    tomcat实现:
    核心类org.apache.catalina.startup.ContextConfig

    //支持注解 see:org.apache.catalina.deploy.WebXml
    protected void processClass(WebXml fragment, JavaClass clazz) {
            AnnotationEntry[] annotationsEntries = clazz.getAnnotationEntries();
            if (annotationsEntries != null) {
                String className = clazz.getClassName();
                for (AnnotationEntry ae : annotationsEntries) {
                    String type = ae.getAnnotationType();
                    if ("Ljavax/servlet/annotation/WebServlet;".equals(type)) {
                        processAnnotationWebServlet(className, ae, fragment);
                    }else if ("Ljavax/servlet/annotation/WebFilter;".equals(type)) {
                        processAnnotationWebFilter(className, ae, fragment);
                    }else if ("Ljavax/servlet/annotation/WebListener;".equals(type)) {
                        fragment.addListener(className);
                    } else {
                        // Unknown annotation - ignore
                    }
                }
            }
        }
    
    //支持web.xml
    protected InputSource getContextWebXmlSource() {
            InputStream stream = null;
            InputSource source = null;
            URL url = null;
    
            String altDDName = null;
    
            // Open the application web.xml file, if it exists
            ServletContext servletContext = context.getServletContext();
            try {
                if (servletContext != null) {
                    altDDName = (String)servletContext.getAttribute(Globals.ALT_DD_ATTR);
                    if (altDDName != null) {
                        try {
                            stream = new FileInputStream(altDDName);
                            url = new File(altDDName).toURI().toURL();
                        } catch (FileNotFoundException e) {
                            log.error(sm.getString("contextConfig.altDDNotFound",
                                                   altDDName));
                        } catch (MalformedURLException e) {
                            log.error(sm.getString("contextConfig.applicationUrl"));
                        }
                    }
                    else {
                        stream = servletContext.getResourceAsStream
                            (Constants.ApplicationWebXml);//Constants.ApplicationWebXml = "/WEB-INF/web.xml"
                        try {
                            url = servletContext.getResource(
                                    Constants.ApplicationWebXml);
                        } catch (MalformedURLException e) {
                            log.error(sm.getString("contextConfig.applicationUrl"));
                        }
                    }
                }
                if (stream == null || url == null) {
                    if (log.isDebugEnabled()) {
                        log.debug(sm.getString("contextConfig.applicationMissing") + " " + context);
                    }
                } else {
                    source = new InputSource(url.toExternalForm());
                    source.setByteStream(stream);
                }
            } finally {
                if (source == null && stream != null) {
                    try {
                        stream.close();
                    } catch (IOException e) {
                        // Ignore
                    }
                }
            }
    
            return source;
        }
    

    springboot中做了增强:
    核心类:org.springframework.boot.web.servlet.ServletRegistrationBean org.springframework.boot.web.servlet.ServletListenerRegistrationBean均实现ServletContextInitializer接口,spring启动tomcat时被调用

    class TomcatStarter implements ServletContainerInitializer {
    
    	private static final Log logger = LogFactory.getLog(TomcatStarter.class);
    
    	private final ServletContextInitializer[] initializers;
    
    	private volatile Exception startUpException;
    
    	TomcatStarter(ServletContextInitializer[] initializers) {
    		this.initializers = initializers;
    	}
    
    	@Override
    	public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
    		try {
    			for (ServletContextInitializer initializer : this.initializers) {
                                    //因为ServletListenerRegistrationBean FilterRegistrationBean ServletListenerRegistrationBean均实现此类,故启动时会被注册到tomcat容器中
    				initializer.onStartup(servletContext);
    			}
    		}
    		catch (Exception ex) {
    			this.startUpException = ex;
    			// Prevent Tomcat from logging and re-throwing when we know we can
    			// deal with it in the main thread, but log for information here.
    			if (logger.isErrorEnabled()) {
    				logger.error("Error starting Tomcat context. Exception: " + ex.getClass().getName() + ". Message: "
    						+ ex.getMessage());
    			}
    		}
    	}
    
    	public Exception getStartUpException() {
    		return this.startUpException;
    	}
    
    }
    

    https://mp.weixin.qq.com/s/Z3ZemxPHBDAUWE-YEHyyDg

  • 相关阅读:
    MySQL数据库封装和分页查询
    程序员的价值在哪里?
    奇葩的程序员
    京东咚咚架构演进
    程序员必看的《黑客帝国》,你看懂了吗?
    微信后台技术“干货们”带来的启发
    drf框架 2 drf框架的请求生命周期(as_view和dispatch方法), 请求、解析、渲染、响应、异常, 序列化组件 ,ORM配置回顾(media文件配置),应用在settings.py中INSTALLED_APPS注册意义 ,数据库配置
    drf框架, 接口(api) Django FBV => CBV drf框架的基础试图类 drf核心组件 群查与单查 python换源
    前端Vue框架 05 第三方插件(vuex: 组件间交互的(移动端), axios
    前端Vue框架 04 路由:逻辑跳转、路由传参 项目组件的数据局部化处理data(){ return{} } 组件的生命周期钩子 组件间通信 全局配置css, js
  • 原文地址:https://www.cnblogs.com/zhangww/p/14991721.html
Copyright © 2011-2022 走看看