简体   繁体   English

如何通过spring boot配置'dispatcherServlet'在启动时加载?

[英]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. 我使用spring-boot-starter-parent作为父项,并添加spring-boot-starter-web作为依赖项。

By add the @SpringBootApplication annotation, it works. 通过添加@SpringBootApplication注释,它可以工作。

But DispatcherServlet need initialization 但是DispatcherServlet需要初始化

     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. 我希望我可以将它的loadonstartup设置为1,并且不想使用这个恼人的BeanNameUrlHandlerMapping ,它拒绝了所有内容,我不打算使用它。

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

I read the java-doc about BeanNameUrlHandlerMapping : 我读了关于BeanNameUrlHandlerMapping的java-doc:

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). 这是org.springframework.web.servlet.DispatcherServlet使用的默认实现,以及org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping(在Java 5及更高版本上)。 Alternatively, SimpleUrlHandlerMapping allows for customizing a handler mapping declaratively. 或者,SimpleUrlHandlerMapping允许以声明方式自定义处理程序映射。

That's all, I just want to change these two thing: 就是这样,我只想改变这两件事:

  1. setLoadonStartup setLoadonStartup
  2. don't use BeanNameUrlHandlerMapping 不要使用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. 似乎这对于更新版本的Spring Boot更容易。 Just adding the property spring.mvc.servlet.load-on-startup=1 works for me. 只需添加属性spring.mvc.servlet.load-on-startup=1对我spring.mvc.servlet.load-on-startup=1

I encountered the same problem with loadOnStartup . 我在loadOnStartup遇到了同样的问题。 I solved it by using a custom BeanFactoryPostProcessor to modify the BeanDefinition of the ServletRegistrationBean that Spring Boot creates for registering the DispatcherServlet . 我通过使用自定义BeanFactoryPostProcessor来解决它,以修改Spring Boot为注册DispatcherServlet创建的ServletRegistrationBeanBeanDefinition

The following code will set loadOnStartup for the DispatcherServlet in a Spring Boot app, when used within an @Configuration class: @Configuration类中使用时,以下代码将为Spring Boot应用程序中的DispatcherServlet设置loadOnStartup

@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. 顺便说一下, BeanNameUrlHandlerMapping在这里是无害的。

It is used to map a Spring Bean to a URL - for example it might be used to support Spring HttpInvoker remoting. 它用于将Spring Bean映射到URL - 例如,它可能用于支持Spring HttpInvoker远程处理。

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. 日志输出中的拒绝行只是意味着它不会将任何Spring bean识别为需要URL映射的bean。 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. 您始终可以将此Bean或其包的日志记录级别设置为INFO或更高版本以删除该消息。 In Spring Boot's application.properties put 在Spring Boot的application.properties

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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