2016-12-29 1 views
0

je suit dans l'application de ressort non-démarrage, l'application de l'oreille avec 2 guerres, Comment puis-je mettre la même chose dans une application de démarrage de printempsParent contexte d'application dans la botte de printemps

Dans la couche de service:

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

guerre1 Initializer avec ParentContext

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

War2 Initializer avec ParentContext

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

Répondre

1

Spring Boot Utilise DispatcherServeletAutoConfig pour initialiser une valeur par défaut DispatcherServlet. Vous devez donc personnaliser le servlet Dispatcher par défaut de la façon suivante:

@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; 
    } 
+0

Merci, vous l'essaierez sous peu et mettre à jour. –