2

I want to know why set this value to 1.I read the api,it means Sets the loadOnStartup priority on the Servlet represented by this dynamic ServletRegistration.Why is 1?

I read a piece of code :

public void onStartup(ServletContext servletContext)
            throws ServletException {
        // TODO Auto-generated method stub
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(WebAppConfig.class);
        servletContext.addListener(new ContextLoaderListener(ctx));

        ctx.setServletContext(servletContext);
        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }
v11
  • 2,124
  • 7
  • 26
  • 54

3 Answers3

4

I'm adding to this answer in case anyone else happens upon this post. Based on the documentation this is more complicated than 1 = true and 0 = false. The value is a priority defining the ORDER in which applications that do initialize on startup will be invoked.

ServletRegistration.Dynamic.setLoadOnStartup

This was covered in this question.

Community
  • 1
  • 1
Kevin Seymour
  • 766
  • 9
  • 25
2

Ahh 1 is the convention for true. 0 is convention for false. You set load on startup for the dispatcher servlet so the spring container will be initialized on app server (tomcat etc) startup.

Terry
  • 911
  • 10
  • 26
1

setLoadOnStartup:

void setLoadOnStartup(int loadOnStartup)

Sets the loadOnStartup priority on the Servlet represented by this dynamic ServletRegistration.

A loadOnStartup value of greater than or equal to zero indicates to the container the initialization priority of the Servlet. In this case, the container must instantiate and initialize the Servlet during the initialization phase of the ServletContext, that is, after it has invoked all of the ServletContextListener objects configured for the ServletContext at their ServletContextListener#contextInitialized method.

If loadOnStartup is a negative integer, the container is free to instantiate and initialize the Servlet lazily.

The default value for loadOnStartup is -1.

A call to this method overrides any previous setting.

 Parameters:
        loadOnStartup - the initialization priority of the Servlet 
    Throws:
        IllegalStateException - if the ServletContext from which this ServletRegistration was obtained has already been initialized
cokeman19
  • 2,405
  • 1
  • 25
  • 40
R. Joy
  • 21
  • 1
  • 2