当前位置: 动力学知识库 > 问答 > 编程问答 >

Camel context started twice spring mvc java config

问题描述:

I have a project spring mvc with camel without any xml config file.

My problem is that there are two context : Spring root WebApplicationContext and Spring FrameworkServlet 'dispatcher', and a camel context is created on every context (camel-1 and camel-2), with the same routes. Thus, if I stop or suspend one route, she is stopped or suspended on only one context (WebApplicationContext). I would like to have the context camel only once. How can i do ?

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()

};

}

}

网友答案:

It's Ok if I change my getServletConfigClasses() :

public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

...
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }
...
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: