gpt4 book ai didi

java - 如何在 Spring Boot 应用程序中配置 DispatcherServlet?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:01:37 25 4
gpt4 key购买 nike

在传统的 Spring Web 应用程序中,是否可以覆盖 AbstractDispatcherServletInitializer.createDispatcherServlet,调用 super.createDispatcherServlet,然后在返回的实例上设置以下初始化参数?

setThreadContextInheritable
setThrowExceptionIfNoHandlerFound

如何在 Spring Boot 应用中实现这一点?

最佳答案

您可以定义自己的配置并实现它,如下所示:

@Configuration
public class ServletConfig {

@Bean
public DispatcherServlet dispatcherServlet() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.setThreadContextInheritable(true);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
return dispatcherServlet;
}

@Bean
public ServletRegistrationBean dispatcherServletRegistration() {

ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet());
registration.setLoadOnStartup(0);
registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);

return registration;
}

关于java - 如何在 Spring Boot 应用程序中配置 DispatcherServlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35121399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com