设计模式containerHttpServlet是不是Singleton设计模式

废话就不多说了,开始。。。

    描述:

    在很多书上都提到HttpServlet都是Singleton模式,因此在Servlet Container里头只会创立一个HttpServlet实例,事实是这样的吗?

    

    代码解析:

public abstract class HttpServlet extends GenericServlet {
    ...
    
    public HttpServlet() {
        // NOOP
    }
    
    ...
}

    我们分析HttpServlet源代码就会发明,其供给了一个public的构造函数,此外也不能找到static的getInstance方法。明显这个和Singleton设计模式有背,那为什么很多书中会提到HttpServlet是Singleton模式。

    

    Java Servlet Specification:

    The servlet declaration which is either via the annotation as described in Chapter 8, “Annotations and pluggability” or part of the deployment descriptor of the Web application containing the servlet, as described in Chapter 14, “Deployment Descriptor”, controls how the servlet container provides instances of the servlet. For a servlet not hosted in a distributed environment (the default), the servlet container must use only one instance per servlet declaration. However, for a servlet implementing the SingleThreadModel interface, the servlet container may instantiate multiple instances to handle a heavy request load and serialize requests to a particular instance.

    每日一道理
谁说人与人隔着遥远的重洋,谁说心与心设着坚固的堤防?十六岁的鸟儿飞上天空,总会找到落脚的枝头。

    

    结论:

    HttpServlet的实现其实并不是Singleton模式,只是很多Servlet Container只创立了一个Servlet实现。

文章结束给大家分享下程序员的一些笑话语录: 马云喜欢把自己包装成教主,张朝阳喜欢把自己包装成明星,李彦宏喜欢把自己包装成的很知性,丁磊喜欢把自己包装的有创意,李开复总摆出一副叫兽的样子。看来的。其实我想说,缺啥补啥,人之常情。

--------------------------------- 原创文章 By
设计模式和container
---------------------------------

原文地址:https://www.cnblogs.com/jiangu66/p/3105078.html