You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by Henryk Konsek <he...@gmail.com> on 2013/10/17 11:07:11 UTC

RouteBuilder autodetection support for Spring JavaConfig

Hi,

I helping friend of mine to setup Camel with Spring JavaConfig
project. What we would like to achieve is to create configuration
which scans the classpath and autowires the RouteBuilders into the
CamelContext. This is pretty common pattern for Spring 3.x
JavaConfig-based applications. In the existing version of JavaConfig
component I needed to autowire the RouteBuilders into collection and
then pass it to the overridden CamelConfiguration#routes() [1] method.

@Configuration
@ComponentScan("com.example.app")
public class ApplicationConfiguration extends CamelConfiguration {

    @Autowired
    private List<RouteBuilder> routes;

    @Override
    public List<RouteBuilder> routes() {
        return routes;
    }

}

As I like the convention of configuration approach and because this
kind of autowiring is pretty common pattern for Spring 3.x
JavaConfig-based applications I changed [2]
CamelConfiguration#routes() definition from abstract to default one
returning all the RouteBuilders reigstered in the Spring
ApplicationContext. This way the existing code overriding the
CamelConfiguration#routes() method will still work as before, however
now you may relay on the classpath scanning to do the job without the
boilerplate needed.

@Configuration
@ComponentScan("com.example.app")
public class ApplicationConfiguration extends CamelConfiguration {

  // No overridden routes() - autodetect RouteBuilders

}

I'll update the documentation to describe the new feature.

Cheers.

[1] http://camel.apache.org/maven/camel-2.12.0/camel-spring-javaconfig/apidocs/org/apache/camel/spring/javaconfig/CamelConfiguration.html
[2] 8a93ae7

-- 
Henryk Konsek
http://henryk-konsek.blogspot.com