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);
}
};
}
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.
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