当前位置: 首页 > 面试题库 >

在Spring Boot中使用多个调度程序Servlet / Web上下文

郑星雨
2023-03-14
问题内容

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)。我认为我必须做两件事:

  1. 将servletContainer(因此MainApiApplication.class配置)移出子上下文,然后
  2. 添加路径映射/ one /-> ApiOne.class和/ two / ApiTwo.class

spring boot是怎么做的?


问题答案:

正如@ josh-ghiloni所说,你需要为ServletRegistrationBean要创建的每个隔离的Web上下文注册一个。你需要从xml或java config类创建应用程序上下文。你可以使用@Import@ComponentScan注释将共享服务添加到父上下文。这是一个例子:

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;
    }
}


 类似资料:
  • 问题内容: 到目前为止,我以前一直认为Web应用程序只能具有我们在 我这样想对吗? 我可以在一个Web应用程序中拥有多个调度程序Servlet吗?如果是,如何? 在什么情况下我们可能需要这样做? 整个Web应用程序中只能有一个应用程序上下文吗? 我们如何定义多个应用程序上下文? 非Spring应用程序中可以存在吗? 问题答案: 一个Web应用程序中可以有多个调度程序servlet吗? Web应用程

  • 问题内容: 我正在研究 Spring MVC ,所以我有一些疑问 因此,我有这个配置类,用于配置处理用户请求的 DispatcherServlet : 我很清楚 DispatcherServlet的 工作方式。我的怀疑与 上下文 概念有关。 1)确切表示 上下文 ?我认为这就像一组具有特定用途的豆类,可以在环境中工作。但是我绝对不正确。 2) 根上下文 和 调度程序servlet上下文有 什么区别

  • 我创建了一个带有父上下文(服务)和子上下文(spring-webmvc控制器)的Spring Boot应用程序: 现在,我想为我的配置添加另一个客户端上下文(和DispatcherServlet)。我想我得做两件事: 将(即mainapiapplication.class配置)移出子上下文,并且 添加路径映射和 春靴的方法是什么?

  • 我希望能够从服务spring bean中动态检索spring web应用程序的“servlet上下文路径”(例如或)。 这样做的原因是,我想在电子邮件中使用这个值,将发送给网站的用户。 编辑:附加要求: 我想知道是否有一种方法可以在应用程序启动时检索上下文路径,并让它在所有时间都可供我的所有服务检索?

  • 问题内容: 以上是我的bean配置,为什么在运行应用程序时出现错误。我的日志文件夹为空… 但是我已经将spring-web-mvc添加到确实包含此类文件的类路径中。 问题答案: 但是我已经将spring-web-mvc添加到确实包含此类文件的类路径中。 您如何向CLASSPATH添加内容? 如果要创建Web应用程序,则唯一合适的方法是将.class文件放在WEB-INF / classes下,并将

  • 我正在使用WebLogic12.2.1,并添加了一个JAX-WS客户机。它在eclipse中运行时运行得很好,但是如果我试图通过管理控制台部署war文件,我会得到:web应用程序webapp.war中的URL模式RegistrationService_V10映射到多个servlet。我能找到对它的引用的唯一地方是名为RegistrationServiceV10的类中的com.oracle.webs