在Spring Boot中使用多个分派器ServletWeb上下文

在Spring Boot中使用多个分派器ServletWeb上下文,第1张

在Spring Boot中使用多个分派器Servlet / Web上下文

正如@ 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;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4928597.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存