如何通过Spring Boot在启动时配置'dispatcherServlet'负载?

编程入门 行业动态 更新时间:2024-10-13 06:17:38
本文介绍了如何通过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:

  • setLoadonStartup
  • 不要使用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而创建的ServletRegistrationBean的ServletRegistrationBean来解决了这个问题.

    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'负载?

    本文发布于:2023-11-25 15:08:37,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1630235.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:负载   启动时   Boot   Spring   dispatcherServlet

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!