简体   繁体   中英

how to configure 'dispatcherServlet' load on startup by spring boot?

I use spring-boot-starter-parent as parent and add spring-boot-starter-web as denpendency.

By add the @SpringBootApplication annotation, it works.

But DispatcherServlet need initialization

     Initializing servlet 'dispatcherServlet'
     FrameworkServlet 'dispatcherServlet': initialization started
     Using MultipartResolver [org.springframework.web.multipart.support.StandardServletMultipartResolver@745f40ac]
     Unable to locate LocaleResolver with name 'localeResolver': using default [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@219fc57d]
     Unable to locate ThemeResolver with name 'themeResolver': using default [org.springframework.web.servlet.theme.FixedThemeResolver@7b4bd6bd]
     Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@71ccfa36]
     Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager@43f3e6a9]
     Published WebApplicationContext of servlet 'dispatcherServlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet]
     FrameworkServlet 'dispatcherServlet': initialization completed in 37 ms

I hope I can set it's loadonstartup by 1, and don't want to use this annoying BeanNameUrlHandlerMapping , it rejected everything and I'm not going to use it.

o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'contextAttributes': no URL paths identified

I read the java-doc about BeanNameUrlHandlerMapping :

This is the default implementation used by the org.springframework.web.servlet.DispatcherServlet, along with org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping (on Java 5 and higher). Alternatively, SimpleUrlHandlerMapping allows for customizing a handler mapping declaratively.

That's all, I just want to change these two thing:

  1. setLoadonStartup
  2. don't use BeanNameUrlHandlerMapping

Beside that, other thing spring boot configure for me is very great, and I want to keep it.

Thank you for any help you can provide.

New reply to old post. Seems this is easier to do with more recent versions of Spring Boot. Just adding the property spring.mvc.servlet.load-on-startup=1 works for me.

I encountered the same problem with loadOnStartup . I solved it by using a custom BeanFactoryPostProcessor to modify the BeanDefinition of the ServletRegistrationBean that Spring Boot creates for registering the DispatcherServlet .

The following code will set loadOnStartup for the DispatcherServlet in a Spring Boot app, when used within an @Configuration class:

@Bean
public static BeanFactoryPostProcessor beanFactoryPostProcessor() {
    return new BeanFactoryPostProcessor() {

        @Override
        public void postProcessBeanFactory(
                ConfigurableListableBeanFactory beanFactory) throws BeansException {
            BeanDefinition bean = beanFactory.getBeanDefinition(
                    DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);

            bean.getPropertyValues().add("loadOnStartup", 1);
        }
    };
}

BTW, the BeanNameUrlHandlerMapping is harmless here.

It is used to map a Spring Bean to a URL - for example it might be used to support Spring HttpInvoker remoting.

The rejection lines in the log output simply mean that it doesn't recognize any of the Spring beans as beans that require a URL mapping. Annoying messages but harmless. You could always set the logging level for this bean or its package to INFO or above to remove the message. In Spring Boot's application.properties put

logging.level.org.springframework.web.servlet.handler=INFO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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