2016-08-01 6 views
1

У меня есть проект spring mvc с верблюдом без файла конфигурации xml.Контекст верблюда начался дважды Весна mvc java config

Моя проблема заключается в том, что существуют два контекста: Spring root WebApplicationContext и Spring FrameworkServlet 'dispatcher', а контекст верблюда создается в каждом контексте (верблюд-1 и верблюд-2) с теми же маршрутами. Таким образом, если я останавливаю или приостанавливаю один маршрут, она останавливается или приостанавливается только в одном контексте (WebApplicationContext). Я бы хотел, чтобы контекстный верблюд был только один раз. Как я могу сделать ?

ApplicationConfiguration.java

package sal.sfs.configuration; 

    import sal.sfs.security.SfsSessionInfo; 
    import org.springframework.context.annotation.Bean; 
    import org.springframework.context.annotation.ComponentScan; 
    import org.springframework.context.annotation.Configuration; 
    import org.springframework.context.annotation.Import; 
    import org.springframework.context.annotation.PropertySource; 
    import org.springframework.web.servlet.ViewResolver; 
    import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
    import org.springframework.web.servlet.view.InternalResourceViewResolver; 

    @Configuration 
    @EnableWebMvc 
    @ComponentScan(basePackages = "sal.sfs") 
    @PropertySource(value = {"classpath:sfs.properties"}) 
    @Import({PersistenceConfiguration.class, SecurityConfiguration.class, SfsCamelConfiguration.class}) 
    public class ApplicationConfiguration extends WebMvcConfigurerAdapter { 

     @Bean(name = "SFS") 
     public ViewResolver viewResolver() { 
      InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
      viewResolver.setSuffix(".zul"); 
      return viewResolver; 
     } 

     /* 
     * Configure ResourceHandlers to serve static resources like CSS/ Javascript etc... 
     */ 
     @Override 
     public void addResourceHandlers(ResourceHandlerRegistry registry) { 
      registry.addResourceHandler("/css/**").addResourceLocations("/css/"); 
      registry.addResourceHandler("/img/**").addResourceLocations("/img/"); 
     } 

    } 

SfsCamelConfiguration.java

package sal.sfs.configuration; 

import java.util.List; 
import javax.annotation.Resource; 
import org.apache.camel.Route; 
import org.apache.camel.spring.javaconfig.CamelConfiguration; 
import org.apache.camel.spring.spi.SpringTransactionPolicy; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Import; 

@Configuration 
@ComponentScan("sal.sfs.camel") 
@Import(PersistenceConfiguration.class) 
public class SfsCamelConfiguration extends CamelConfiguration { 

    @Resource 
    PersistenceConfiguration persistenceConfiguration; 

    @Bean 
    public SpringTransactionPolicy camelTransactionPolicy() { 
     SpringTransactionPolicy springTransactionPolicy = new SpringTransactionPolicy(); 
     springTransactionPolicy.setTransactionManager(persistenceConfiguration.transactionManager(persistenceConfiguration.entityManagerFactory().getObject())); 
     springTransactionPolicy.setPropagationBehaviorName("PROPAGATION_REQUIRED"); 
     return springTransactionPolicy; 
    } 

} 

SecurityWebApplicationInitializer.java

package civadis.salaires.sfs.configuration; 

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; 

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { 

} 

SpringMvcInitializer.java

package sal.sfs.configuration; 

import javax.servlet.Filter; 
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter; 
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 

public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class[]{ApplicationConfiguration.class}; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class[]{ApplicationConfiguration.class}; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return new String[]{"/", "*"}; 
    } 


    /* 
    Also we have registered OpenEntityManagerInViewFilter to enable lazy loading of JPA entity graphs in view rendering phase 
    */ 
    @Override 
    protected Filter[] getServletFilters() { 
     return new Filter[]{ 
      new OpenEntityManagerInViewFilter() 
     }; 
    }  
} 
+0

Какую версию Camel вы используете? Попробуйте с последней версией –

+0

Привет, Клаус, я использую последнюю версию верблюда 2.17.2 с весной версии 4.2.5. – catciv

ответ

0

Это нормально, если я меняю getServletConfigClasses():

public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

... 
    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return null; 
    } 
... 
}