318.3. 如何从其他 XML 文件导入路由
从 Camel 2.3 开始提供
当使用 Xml 配置 在 Camel 中定义路由时,您可能希望在其他 XML 文件中定义一些路由。例如,您可能有多个路由,如果某些路由位于单独的 XML 文件中,则可能有助于维护应用程序。您也可以将通用路由和可重复使用的路由存储在其他 XML 文件中,您可以根据需要简单地导入。
					在 Camel 2.3 中,您现在可以在新的 < route  以外的路由。
				Context/> 标签中定义在 <camel Context/>
注意: 当您使用 <routeContext> 后,它们会被分离,且无法重复使用现有的 <onException>、<intercept>、<dataFormats> 和类似在 <camelContext> 中定义的跨组合功能。换句话说,<routeContext> 当前被隔离。这可能会在 Camel 3.x 中有所变化。
					例如,我们可以有一个名为 myCoolRoutes.xml 的文件,其中包含几个路由,如下所示:
				
myCoolRoutes.xml
					然后,在包含 CamelContext 的 XML 文件中,您可以使用 Spring 导入 myCoolRoute.xml 文件。
然后,在 <camelContext/& gt; 中,您可以使用其 id 引用 & lt;routeContext />,如下所示:
				
另请注意,您可以混合和匹配,在 CamelContext 中具有路由,也可以在 RouteContext 中进行外部化。
					您可以根据需要有许多 < ;routeContextRef/& gt;。
				
可重复使用的路由
					< routeContext/& gt; 中定义的路由可以被多个 < camelContext/> 重复使用。但是,它只是被重复使用的定义。在运行时,每个 CamelContext 将基于定义创建自己的路由实例。
				
318.3.1. 测试时间排除。
在测试时,通常需要选择性地将匹配的路由排除在 initalized 中,这些路由不适用于测试场景或很有用。例如,您可能会在 'org.example.routes' 软件包中有一个 spring 上下文文件 routes-context.xml 和三个 Route builders RouteA、RouteB 和 RouteC。packageScan 定义将发现所有三个路由并初始化它们。
假设 RouteC 不适用于我们的测试场景,并在测试期间生成很多 noise。从这个特定测试中排除此路由会很好。SpringTestSupport 类已修改为允许这一点。它提供两种方法(excludedRoute 和 excludedRoutes),它们可以被覆盖来排除单个类或类数组。
为了让 hook 到 camelContext 初始化中,spring 会排除 MyExcludedRouteBuilder.class,我们需要截获 spring 上下文创建。当覆盖 createApplicationContext 以创建 spring 上下文时,我们调用 getRouteExcludingApplicationContext ()方法,以提供处理排除的特殊父 spring 上下文。
@Override
protected AbstractXmlApplicationContext createApplicationContext() {
    return new ClassPathXmlApplicationContext(new String[] {"routes-context.xml"}, getRouteExcludingApplicationContext());
}
@Override
protected AbstractXmlApplicationContext createApplicationContext() {
    return new ClassPathXmlApplicationContext(new String[] {"routes-context.xml"}, getRouteExcludingApplicationContext());
}现在,RouteC 将不包括在初始化中。同样,在只测试 RouteC 的另一个测试中,我们可以通过覆盖来排除 RouteB 和 RouteA
@Override
protected Class[] excludeRoutes() {
    return new Class[]{RouteA.class, RouteB.class};
}
@Override
protected Class[] excludeRoutes() {
    return new Class[]{RouteA.class, RouteB.class};
}