java – 在spring应用程序中使用多个调度程序servlet

前端之家收集整理的这篇文章主要介绍了java – 在spring应用程序中使用多个调度程序servlet前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在我的spring应用程序中,我有以下Spring环境配置类:

WebAppInitializer.java

@H_403_7@@Order(value=1) public class WebAppInitializer implements WebApplicationInitializer { @SuppressWarnings("resource") @Override public void onStartup(ServletContext container) { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(WebAppConfig.class); // Manage the lifecycle of the root application context //container.addListener(new ContextLoaderListener(rootContext)); // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(DispatcherConfig.class); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher",new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }

WebAppConfig.java

@H_403_7@@EnableWebMvc @EnableTransactionManagement(mode=AdviceMode.PROXY,proxyTargetClass=true) @ComponentScan(value="spring.webapp.lojavirtual") @Configuration public class WebAppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/bootstrap/**").addResourceLocations("/bootstrap/").setCachePeriod(31556926); registry.addResourceHandler("/extras/**").addResourceLocations("/extras/").setCachePeriod(31556926); registry.addResourceHandler("/jquery/**").addResourceLocations("/jquery/").setCachePeriod(31556926); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }

DispatcherConfig.java

@H_403_7@@Controller @Import(WebAppConfig.class) public class DispatcherConfig { @Bean public ViewResolver jspResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/jsp/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }

我想在我的应用程序中添加其他调度程序servlet.我的第一个想法是将以下代码添加到上面的类中:

在WebAppInitializer.java中

像这样的新块,在适当的位置更改名称

@H_403_7@// Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(DispatcherConfig.class); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher",new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/");

添加一个新的类,如DispatcherConfig.java,其名称在上面的代码中选择.

我的问题是:

1)首先,我的方法添加新的调度程序servlet的正确方法

2)其次,如果问题1的答案为“是”,我应该在WebAppInitializer中更改哪些名称

3)在我的控制器中,我如何确定我的请求应该去哪个调度程序servlet?我的控制器使用如下方法调用视图:

@H_403_7@@RequestMapping(value="view_mapping") public method() { ModelAndView mav = new ModelAndView() mav.setViewName("view_name"); return mav; }
最佳答案
您可以根据需要拥有尽可能多的DispatcherServlet.基本上你需要做的是复制配置并给servlet一个不同的名称(否则它将覆盖前一个),并为它提供一些单独的配置类(或xml文件).

您的控制器不应该关心它们运行的​​DispatcherServlet,如果您包含用于检测它的代码(如果您添加另一个,那么您将需要继续修改控制器以解决该问题).

但是,虽然通常可以有多个servlet,但是不需要多个servlet,您可以使用DispatcherServlet的单个实例来处理它.

猜你在找的Spring相关文章