在 Spring Boot 中使用多个调度程序 servlet/web 上下文 [英] Using multiple dispatcher servlets / web contexts with spring boot

查看:46
本文介绍了在 Spring Boot 中使用多个调度程序 servlet/web 上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有父上下文(服务)和子上下文(spring-webmvc 控制器)的 spring boot 应用程序:

I created a spring boot application with a parent context (services) and child context (spring-webmvc controllers):

@Configuration
public class MainApiApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder()
                .parent(Services.class)
                .child(ApiOne.class, MainApiApplication.class)
                .run(args);
    }

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        return new TomcatEmbeddedServletContainerFactory();
    }

}

现在我想为我的 ApiTwo.class 配置添加另一个客户端上下文(和 DispatcherServlet).我想我必须做两件事:

Now I want to add another client context (and DispatcherServlet) for my ApiTwo.class configuration. I think I have to do two things:

  1. servletContainer(即 MainApiApplication.class 配置)移出子上下文并
  2. 添加路径映射 /one/->ApiOne.class/two/ApiTwo.class
  1. Move the servletContainer (thus the MainApiApplication.class configuration) out of the child context and
  2. add a path mapping /one/ -> ApiOne.class and /two/ ApiTwo.class

spring boot 的做法是什么?

What is the spring boot way to do it?

推荐答案

正如@josh-ghiloni 已经说过的,您需要为每个要创建的独立 Web 上下文注册一个 ServletRegistrationBean.您需要从 xml 或 java 配置类创建应用程序上下文.您可以使用 @Import@ComponentScan 批注将共享服务添加到父上下文.下面是一个例子:

As @josh-ghiloni already said, you need to register a ServletRegistrationBean for every isolated web context you want to create. You need to create an application context from a xml or java config class. You can use @Import and @ComponentScan annotation to add shared services to the parent context. Here is an example:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;


//@ComponentScan({"..."})
//@Import({})
public class Starter {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Starter.class, args);
    }

    @Bean
    public ServletRegistrationBean apiV1() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet();

        XmlWebApplicationContext applicationContext = new XmlWebApplicationContext();
        applicationContext.setConfigLocation("classpath:/META-INF/spring/webmvc-context.xml");
        dispatcherServlet.setApplicationContext(applicationContext);

        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/api/1/*");
        servletRegistrationBean.setName("api-v1");

        return servletRegistrationBean;
    }

    @Bean
    public ServletRegistrationBean apiV2() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet();

        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(ResourceConfig.class);
        dispatcherServlet.setApplicationContext(applicationContext);

        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/api/2/*");
        servletRegistrationBean.setName("api-v2");
        return servletRegistrationBean;
    }
}

这篇关于在 Spring Boot 中使用多个调度程序 servlet/web 上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆