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

前端 未结 3 883
暖寄归人
暖寄归人 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);
            }
        };
    }
    

提交回复
热议问题