如何通过Spring Boot在启动时配置'dispatcherServlet'负载? [英] how to configure 'dispatcherServlet' load on startup by spring boot?

查看:2095
本文介绍了如何通过Spring Boot在启动时配置'dispatcherServlet'负载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用spring-boot-starter-parent作为父级,并添加spring-boot-starter-web作为依赖项.

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

通过添加@SpringBootApplication批注,它可以正常工作.

By add the @SpringBootApplication annotation, it works.

但是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


我希望我可以将loadonstartup设置为1,并且不想使用它 令人讨厌的BeanNameUrlHandlerMapping,它拒绝了所有内容,我将不使用它.


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.

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

我阅读了有关BeanNameUrlHandlerMapping的Java文档:

I read the java-doc about BeanNameUrlHandlerMapping:

这是org.springframework.web.servlet.DispatcherServlet和org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping(在Java 5及更高版本上)一起使用的默认实现.另外,SimpleUrlHandlerMapping允许声明性地自定义处理程序映射.

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). Alternatively, SimpleUrlHandlerMapping allows for customizing a handler mapping declaratively.

仅此而已,我只想更改这两件事:

That's all, I just want to change these two thing:

  1. setLoadonStartup
  2. 不要使用BeanNameUrlHandlerMapping

除此之外,spring boot为我配置的其他东西也很棒,我想保留它.

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.

推荐答案

我遇到了与loadOnStartup相同的问题.我通过使用自定义BeanFactoryPostProcessor修改了Spring Boot为注册DispatcherServlet而创建的ServletRegistrationBeanServletRegistrationBean来解决了这个问题.

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.

@Configuration类中使用以下代码时,将在Spring Boot应用程序中为DispatcherServlet设置loadOnStartup:

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

这篇关于如何通过Spring Boot在启动时配置'dispatcherServlet'负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆