使用 JAVA 中的注释在启动时加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18808853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:15:17  来源:igfitidea点击:

Load On Start Up using Annotation in JAVA

javatomcatservletsannotations

提问by Diego Alves

I have this code,

我有这个代码

@WebServlet(value="/initializeResources", loadOnStartup=1)
public class InitializeResources extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      System.out.println("HEREEEE");
  }

}

But the servlet doesn't start when the web application is started.

但是 servlet 不会在 Web 应用程序启动时启动。

How use load on startup on Servlet Annotation?

如何在 Servlet Annotation 上使用启动时加载?

My Servlet API is 3.0 and I use Tomcat 7

我的 Servlet API 是 3.0,我使用 Tomcat 7

采纳答案by Paul Vargas

With you current code, you need to do a GET request for see the output HEREEEE.

使用当前代码,您需要执行 GET 请求以查看输出HEREEEE

If you want to do something on the startup of the servlet (i.e. the element loadOnStartupwith value greater or equal to zero, 0), you need put the code in a init method or in the constructor of the servlet:

如果你想在 servlet 启动时做一些事情(即loadOnStartup值大于或等于 0的元素,0),你需要将代码放在 init 方法或 servlet 的构造函数中:

@Override
public void init() throws ServletException {
    System.out.println("HEREEEE");
}


It may be more convenient to use a listener to start a resource in the application scope(in the ServletContext).

使用侦听器在应用程序范围内(在 中ServletContext)启动资源可能更方便。

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class InitializeListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("On start web app");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("On shutdown web app");
    }

}

For an example, see my answer for the question Share variables between JAX-RS requests.

例如,请参阅我对问题Share variables between JAX-RS requests 的回答。

回答by Ravi Thapliyal

When loadOnStartupis specified for a Servlet, the container would only load and pre-instantiate an instance of your Servlet ready to process any GET/POST requests that may come. This by itself wouldn't cause doGet()or doPost()to get fired because an actual client request hasn't come for processing yet. So, what's its use then?

loadOnStartup为 Servlet 指定 时,容器将仅加载和预实例化 Servlet 的实例,以准备处理可能出现的任何 GET/POST 请求。这本身不会导致doGet()或被doPost()解雇,因为实际的客户端请求尚未进行处理。那么,它有什么用呢?

Well, loadOnStartupis typically used for Servlets that have heavy initialization code; say, they may make a JNDI call to get hold of a resource or a Database call to populate a local data structure with some backend values. In the absence of loadOnStartupthe very first client request could be painfully slow because of all this extra initialization stuff and hence pre-instantiating it makes sense.

嗯,loadOnStartup通常用于具有大量初始化代码的 Servlet;比如说,他们可能会通过 JNDI 调用来获取资源或通过数据库调用来使用一些后端值填充本地数据结构。在没有loadOnStartup第一个客户端请求的情况下,由于所有这些额外的初始化内容,可能会非常缓慢,因此预先实例化它是有意义的。

Now, your custom initialization code (JNDI, JDBC) would go in an overriden GenericServlet#init()method which is called by the servlet container to indicate to a servlet that it's being placed into service.

现在,您的自定义初始化代码(JNDI、JDBC)将进入覆盖GenericServlet#init()方法,该方法由 servlet 容器调用,以向 servlet 指示它正在投入服务。

回答by Iljaminati

@WebServlet(name="InitializeResources", urlPatterns="/initializeResources", loadOnStartup=1)

urlPatterns to be ensure that the web conatainer finds the servlet path.

urlPatterns 以确保 Web 容器找到 servlet 路径。