java - Spring boot 中的父应用程序上下文

标签 java spring spring-mvc spring-boot spring-annotations

我在非启动 spring 应用程序中具有以下内容,ear 应用程序有 2 个 war ,如何在 spring-boot 应用程序中实现相同的内容

在服务层:

    @Configuration
    @ComponentScan("")
    @Import({..})
    public class BaseConfig implements ApplicationContextAware {
    private ApplicationContext parentContext;
    @Bean(name = "earParentContext")
    public ApplicationContext parentContextKey() {
        return this.parentContext;
    }

    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        this.parentContext = applicationContext;
    }
}

beanRefContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <context:annotation-config />
  <context:component-scan resource-pattern="BaseConfig.class" base-package="com.pkg.sa.config"
    annotation-config="true" />
</beans>

带有 ParentContext 的 War1 初始化程序

public class War1Initializer implements WebApplicationInitializer {
@Override
    public void onStartup(final ServletContext container) throws ServletException {
    ServletRegistration.Dynamic war1Servlet = container.addServlet("war1Dispatcher", new DispatcherServlet());
        war1Servlet.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
        war1Servlet.setInitParameter("parentContextKey", "earParentContext");
        war1Servlet.setLoadOnStartup(1);
        war1Servlet.addMapping("/");

        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.setDisplayName("AbcNx");

        // Registers the application configuration with the root context
        BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance("classpath:beanRefContext.xml");
        BeanFactoryReference parentContextRef = locator.useBeanFactory("earParentContext");
        ApplicationContext parentContext = (ApplicationContext) parentContextRef.getFactory();
        rootContext.setParent(parentContext);

        rootContext.register(War1WebMvcConfigurer.class);
    }
}

带有 ParentContext 的 War2 初始化器

public class War2Initializer implements WebApplicationInitializer {
@Override
    public void onStartup(final ServletContext container) throws ServletException {
    ServletRegistration.Dynamic war2Servlet = container.addServlet("war2Dispatcher", new DispatcherServlet());
        war2Servlet.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
        war2Servlet.setInitParameter("parentContextKey", "earParentContext");
        war2Servlet.setLoadOnStartup(1);
        war2Servlet.addMapping("/");

        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.setDisplayName("AbcNx");

        // Registers the application configuration with the root context
        BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance("classpath:beanRefContext.xml");
        BeanFactoryReference parentContextRef = locator.useBeanFactory("earParentContext");
        ApplicationContext parentContext = (ApplicationContext) parentContextRef.getFactory();
        rootContext.setParent(parentContext);

        rootContext.register(War2WebMvcConfigurer.class);
    }
}

最佳答案

Spring Boot 使用 DispatcherServletAutoConfig 来初始化默认的 DispatcherServlet。因此,您需要按以下方式自定义默认的 DispatcherServlet :

    @Bean
    public DispatcherServlet dispatcherServlet()
    {
        DispatcherServlet servlet = new DispatcherServlet();

        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.setDisplayName("Self Administration Nx");

        // Registers the application configuration with the root context
        rootContext.setConfigLocation("com.xyz.mnp.config");
        BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance("classpath:beanRefContext.xml");
        BeanFactoryReference parentContextRef = locator.useBeanFactory("sharedContext");
        ApplicationContext parentContext = (ApplicationContext) parentContextRef.getFactory();
        rootContext.setParent(parentContext);
        rootContext.register(WebConfigurer.class);
        servlet.setApplicationContext(rootContext);
        return servlet;
    }

关于java - Spring boot 中的父应用程序上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41388665/

相关文章:

java - 如何在 Spring Integration 中轮询时根据主题过滤电子邮件

spring - 是否可以在表单 :select/form:option 的 itemLabel 中使用多个参数

javascript - 无法在 spring security 3 中允许静态资源

java - 如何在java中重新定义lambda匿名类

java - 关于Spring bean生命周期初始化顺序的困惑

java - 有没有一种方法可以让别人在看不到代码的情况下运行我的 Java 程序?

java - Spring:单例/ session 范围和并发

java - 在 Junit 测试中禁用 Spring @EnableScheduling

java - Logback SMTPAppender 限制速率

java - Android 从两个 Activity 传递哈希表