Hi Michael,

Interesting case. As far as I know, you cannot change the order of the
components to load while scanning.

It's maybe possible to load the routetemplate definitions before Spring
starts:

1. Move the routetemplate definitions (for example the RouteTemplate.java
file) to a separate package.
2. Load that package manually:

@SpringBootApplication
public class MyCamelApplication {

    /**
     * A main method to start this application.
     */
    public static void main(String[] args) {
        setRouteTemplates("org.myorg.routetemplates");
        SpringApplication.run(MyCamelApplication.class, args);
    }

//loads templates in the template package
public void setRouteTemplates(String packageName) throws Exception {

// create scanner and disable default filters (that is the 'false' argument)
final ClassPathScanningCandidateComponentProvider provider = new
ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new
RegexPatternTypeFilter(Pattern.compile(".*")));

// get matching classes defined in the package
final Set<org.springframework.beans.factory.config.BeanDefinition> classes
= provider.findCandidateComponents(packageName);

// this is how you can load the class type from BeanDefinition instance
for (BeanDefinition bean: classes) {
Class<?> clazz = Class.forName(bean.getBeanClassName());
Object template = clazz.getDeclaredConstructor().newInstance();
if(template instanceof RouteBuilder){
context.addRoutes((RouteBuilder) template);
}

}

}

}

I haven't tested it, but it is worth a try.

Raymond

On Wed, Nov 2, 2022 at 2:12 PM Michael Rambichler <mich...@rambichler.at>
wrote:

> Hi all,
>
> we are heavily using route templates with camel 3.18.x and Spring boot
> 2.7.3
>
> I still wonder if there is no better possibility to avoid the spring boot
> component scan order dependency.
>
> To reproduce the issue: check out the example from
>
> https://github.com/apache/camel-spring-boot-examples/tree/main/routetemplate
> and rename the MyRouteTemplates.java to RouteTemplates.java
>
> There we are, your spring boot will initialize your RouteTemplate
> (RouteTemplates.class) class* after* the RouteBuilder
> (MyTemplateBuilder.class) and fails with: Cannot find RouteTemplate with id
> myTemplate
>
> Are there any better approaches to avoid this Spring Component Scan
> ordering?
>
> BR
>  Michael
>

Reply via email to