I use spring-boot-starter-parent
as parent and add spring-boot-starter-web
as denpendency.
By add the @SpringBootApplication
annot
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);
}
};
}