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

前端 未结 3 887
暖寄归人
暖寄归人 2021-02-08 11:29

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

By add the @SpringBootApplication annot

相关标签:
3条回答
  • 2021-02-08 12:11

    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);
            }
        };
    }
    
    0 讨论(0)
  • 2021-02-08 12:20

    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.

    0 讨论(0)
  • 2021-02-08 12:24

    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

    0 讨论(0)
提交回复
热议问题