java 如何通过spring boot在启动时配置“dispatcherServlet”负载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31322670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to configure 'dispatcherServlet' load on startup by spring boot?
提问by PhoenixYip
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). Alternatively, SimpleUrlHandlerMapping allows for customizing a handler mapping declaratively.
这是 org.springframework.web.servlet.DispatcherServlet 以及 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping(在 Java 5 及更高版本上)使用的默认实现。或者, SimpleUrlHandlerMapping 允许以声明方式自定义处理程序映射。
That's all, I just want to change these two thing:
就是这样,我只想改变这两件事:
- setLoadonStartup
- don't use BeanNameUrlHandlerMapping
- 设置加载启动
- 不要使用 BeanNameUrlHandlerMapping
Beside that, other thing spring boot configure for me is very great, and I want to keep it.
除此之外,spring boot 配置对我来说非常棒,我想保留它。
Thank you for any help you can provide.
感谢您提供任何帮助。
采纳答案by Erin Drummond
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
.
我遇到了同样的问题loadOnStartup
。我通过使用自定义解决它BeanFactoryPostProcessor
修改BeanDefinition
的ServletRegistrationBean
那个春天启动创建用于注册DispatcherServlet
。
The following code will set loadOnStartup
for the DispatcherServlet
in a Spring Boot app, when used within an @Configuration
class:
下面的代码将设置loadOnStartup
为DispatcherServlet
在Spring应用程序启动,一个中使用时@Configuration
类:
@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);
}
};
}
回答by Bal
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.
旧帖子的新回复。使用更新版本的 Spring Boot 似乎更容易做到这一点。只需添加属性spring.mvc.servlet.load-on-startup=1
对我有用。
回答by paulchapman
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. 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
日志输出中的拒绝行仅仅意味着它没有将任何 Spring bean 识别为需要 URL 映射的 bean。烦人的消息,但无害。您始终可以将此 bean 或其包的日志记录级别设置为 INFO 或更高以删除消息。在 Spring Boot 的application.properties
put 中
logging.level.org.springframework.web.servlet.handler=INFO
logging.level.org.springframework.web.servlet.handler=INFO