简体   繁体   English

servlet中的参数化构造函数

[英]Parameterized constructor in servlet

Can I declare parameterized constructor inside servlet which is only constructor ? 我可以在servlet中声明参数化构造函数,它只是构造函数吗?

If no then why ? 如果没有那么为什么?

No. 没有。

Servlet instances are created by the container via reflection, and they expect to find a public, no-arg constructor (the default constructor). Servlet实例由容器通过反射创建,他们希望找到一个public,no-arg构造函数(默认构造函数)。

To configure your servlet, use servlet parameters specified in the web.xml file. 要配置servlet,请使用web.xml文件中指定的servlet参数。 These are passed to your servlet's init() method. 这些传递给servlet的init()方法。


While it would be possible for a servlet container to choose a non-default constructor and coerce character strings to simple types and invoke the constructor reflectively, this isn't what the Servlet specification requires. 虽然这将有可能为一个servlet容器选择一个非默认的构造函数,并强制对字符串进行简单的类型和反思调用构造函数,这不是什么Servlet规范要求。

Part of the reason may be historical; 部分原因可能是历史性的; servlets were first specified long before dependency injection systems made this alternative widely practiced. 在依赖注入系统使这种替代方案得到广泛实践之前,servlet首次被指定。 However, such constructors would be fairly limited; 但是,这样的构造函数相当有限; it would be practical to pass arguments that can be created from a simple character string specified in the web.xml, but more useful objects—a DataSource , for example—would be awkward. 传递可以从web.xml中指定的简单字符串创建的参数是切实可行的,但更有用的对象 - 例如DataSource - 会很尴尬。

It would be nice to have final member variables in a servlet though. 最好在servlet中包含最终成员变量。

The JSR formerly known as "WebBeans" (JSR 299, I think), will provide some standards for dependency injection support in Servlets. JSR以前称为“WebBeans”(我认为是JSR 299),它将为Servlet中的依赖注入支持提供一些标准。 This might address some of the drawbacks in the current approach. 这可能解决当前方法中的一些缺点。

no! 没有! we cannot provide a parameterized constructor in servlet.The servlet container creates the object for sevrlet.The container will create the object based on Class.forName(String classname) . 我们不能在servlet中提供参数化构造函数.servlet容器为sevrlet创建对象。容器将基于Class.forName(String classname)创建对象。 we can create an object to a class using Class.forName() , if the class contains default constructor only.since the container uses the Class.forName() code in creating object, we do not write parameterized constructor as part out servlet. 我们可以使用Class.forName()创建一个类的对象,如果该类包含默认构造函数only。因为容器在创建对象时使用Class.forName()代码,我们不会将参数化构造函数写为部分输出servlet。 Even though if we want to write parameterized constructor, we have call the constructor from service() method! 即使我们想编写参数化构造函数,我们也可以从service()方法调用构造函数!

Since servlets are instantiated by the container they need a no-argument constructor. 由于servlet由容器实例化,因此它们需要一个无参数的构造函数。

Additionally the container may reuse servlets and will not call the constructor on reuse. 此外,容器可以重用servlet,并且不会在重用时调用构造函数。

您需要通过<servlet-param>初始化变量或使用类似Spring的框架,它允许您将Servlet代理到与其他任何bean一样的其他类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM