Camel上下文启动了两次spring mvc java config

时间:2016-08-01 12:02:31

标签: apache-camel spring-java-config

我有一个项目spring mvc with camel没有任何xml配置文件。

我的问题是有两个上下文:Spring root WebApplicationContext和Spring FrameworkServlet' dispatcher',并且在每个上下文(camel-1和camel-2)上创建了一个camel上下文,具有相同的路由。因此,如果我停止或暂停一个路由,则仅在一个上下文(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()
        };
    }    
}

1 个答案:

答案 0 :(得分:0)

如果我改变了我的getServletConfigClasses(),那就没关系了:

public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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