camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bwtaylor <>
Subject Re: Controlling the ApplicationContext used by a RouteBuilder
Date Sat, 07 Jun 2008 05:35:40 GMT

I feel silly. I just came up with a way to do what I want in pure spring. The
solution I was proposing with  CamelContextFactoryBean, doesn't actually use
any aspect of the Camel code. You can attach an AC to a new parent in
standalone code, like so:

package utility.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;

public class ParentInjector implements ApplicationContextAware {
    ConfigurableApplicationContext applicationContext;
    ApplicationContext parentApplicationContext;

    public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
        if  (  (ConfigurableApplicationContext.class).isAssignableFrom(
applicationContext.getClass() ) ) {
            this.applicationContext =
        } else {
            throw new BeanNotOfRequiredTypeException("<ApplicationContext>",
ConfigurableApplicationContext.class, applicationContext.getClass());
    public void setParentApplicationContext(ApplicationContext
parentApplicationContext) throws BeansException {
        this.parentApplicationContext = parentApplicationContext;        

    private void attachParent() {
        if (applicationContext != null && parentApplicationContext !=null)       


Then in ANY xml based config file, you can add a bean like so

    <bean class="utility.spring.ParentInjector">
      <property name="parentApplicationContext">
        <bean class="utility.spring.MyFavSpringLocatorOrFactory"
           <constructor-arg value="theNameOfMyContext"/> 

Where utility.spring.MyFavSpringLocatorOrFactory is a class that locates,
builds, borrows, or steals an ApplicationContext object in any fashion it
pleases, xml based or not. 

James.Strachan wrote:
> BTW I'm still not quite sure why you couldn't just have this inside
> activemq.xml
> <include uri="myroutes.xml"/>
> then in myroutes.xml have a <camelContext> and whatever other routes you
> want?

For example, suppose I want to make a message enricher, and my route will be
something like:

Suppose the "enricher" bean is a camel Processor that will delegate to a
bunch of preexisting beans, like my data access layer, defined in some
ApplicationContext that is not 100% XML based. Maybe we used the grails
spring builder or spring's java config for all or part of it. Maybe I have
mostly XML, except that I do a little bit with java code that uses Spring
APIs to manipulate the AC before it's offered up for consumption. Maybe I'm
constructing my AC dynamically from a registry using mule's

I could port the "enricher" bean to an XML config, but I'd have to do so for
it and all it's bean dependencies, which might be hard to accomplish. If I
succeed, then I'm double maintaining them, violating DRY. It might not even
be possible to know what the dependencies are: if I use something like a
strategy pattern with <property name="strategy"
value="${}" , a plugin system, or a registry
indirection like GalaxyApplicationContext, they could be changing without my
View this message in context:
Sent from the Camel - Users mailing list archive at

View raw message