加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:48:21 所属栏目:大数据 来源:网络整理
导读:在我的spring应用程序中,我有以下Spring环境配置类: WebAppInitializer.java @Order(value=1)public class WebAppInitializer implements WebApplicationInitializer { @SuppressWarnings("resource") @Override public void onStartup(ServletContext cont

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

WebAppInitializer.java

@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

@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

@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中

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

// 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?我的控制器使用如下方法调用视图:

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读