这是用户在 2024-3-28 22:14 为 https://camel.apache.org/manual/spring-xml-extensions.html 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

Spring XML 春天 XML

Using Camel with Spring XML files, is a classic way, of using XML DSL with Camel. Camel has historically been using Spring XML for a long time. The Spring framework started with XML files as a popular and common configuration for building Spring applications.
使用 Camel 与 Spring XML 文件是一种经典的方式,使用 XML DSL 与 Camel 一起使用。Camel 长期以来一直在使用 Spring XML。Spring 框架最初使用 XML 文件作为构建 Spring 应用程序的流行和常见配置。

The following is an example of what it looks like:
以下是一个示例,看起来是这样的:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
    ">

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="direct:a"/>
            <choice>
                <when>
                    <xpath>$foo = 'bar'</xpath>
                    <to uri="direct:b"/>
                </when>
                <when>
                    <xpath>$foo = 'cheese'</xpath>
                    <to uri="direct:c"/>
                </when>
                <otherwise>
                    <to uri="direct:d"/>
                </otherwise>
            </choice>
        </route>
    </camelContext>

</beans>

Using Spring XML 使用 Spring XML

The following dependency needs to be added to your pom.xml so that Spring XML files can be scanned by Camel:
需要将以下依赖项添加到您的 pom.xml 中,以便 Camel 可以扫描 Spring XML 文件:

 <dependency>
     <groupId>org.apache.camel.springboot</groupId>
     <artifactId>camel-spring-boot-xml-starter</artifactId>
 </dependency>

You can use Spring XML files to specify Camel routes using XML DSL as shown:
您可以使用 Spring XML 文件来指定使用 XML DSL 的 Camel 路由,如下所示:

<camelContext id="camel-A" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="seda:start"/>
    <to uri="mock:result"/>
  </route>
</camelContext>

Configuring Components and Endpoints
配置组件和端点

You can configure your Component or Endpoint instances in your Spring XML as follows in this example.
在这个示例中,您可以通过以下方式在 Spring XML 中配置您的组件或端点实例。

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
</camelContext>

<bean id="activemq" class="org.apache.camel.component.activemq.ActiveMQComponent">
  <property name="connectionFactory">
    <bean class="org.apache.activemq.ActiveMQConnectionFactory">
      <property name="brokerURL" value="tcp:someserver:61616"/>
    </bean>
  </property>
</bean>

Which allows you to configure a component using any name, but its common to use the same name eg (activemq). Then you can refer to the component using activemq:destinationName.
允许您使用任何名称配置组件,但通常使用相同的名称,例如( activemq )。然后,您可以使用 activemq:destinationName 引用该组件。

This works by the Camel lazily fetching components from the Spring context for the scheme name you use for Endpoint URIs.
这是通过骆驼从 Spring 上下文中惰性地获取组件来实现的,用于 Endpoint URI 的方案名称。

Using Java DSL with Spring XML files
使用 Java DSL 与 Spring XML 文件

You can use Java Code to define your RouteBuilder implementations. These can be defined as beans in spring and then referenced in your camel context e.g.
您可以使用 Java 代码来定义您的 RouteBuilder 实现。这些可以在 Spring 中定义为 bean,然后在 Camel 上下文中引用。

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <routeBuilder ref="myBuilder"/>
</camelContext>

<bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/>

Using package scanning 使用包扫描

Camel also provides a powerful feature that allows for the automatic discovery and initialization of routes in given packages. This is configured by adding tags to the camel context in your spring context definition, specifying the packages to be recursively searched for RouteBuilder implementations. To use this feature in 1.X, requires a <package></package> tag specifying a comma separated list of packages that should be searched e.g.
骆驼还提供了一个强大的功能,可以自动发现和初始化给定包中的路由。这是通过在 Spring 上下文定义中向骆驼上下文添加标签来配置的,指定要递归搜索 RouteBuilder 实现的包。要在 1.X 中使用此功能,需要使用<package></package>标签指定一个逗号分隔的包列表,这些包将被搜索。

<camelContext>
  <packageScan>
    <package>com.foo</package>
    <excludes>**.*Excluded*</excludes>
    <includes>**.*</includes>
  </packageScan>
</camelContext>

This will scan for RouteBuilder classes in the com.foo and sub-packages.
这将扫描 com.foo 和其子包中的 RouteBuilder 类。

You can also filter the classes with includes or excludes such as:
您还可以使用包含或排除的方式来筛选课程

<camelContext>
  <packageScan>
    <package>com.foo</package>
    <excludes>**.*Special*</excludes>
  </packageScan>
</camelContext>

Which will skip classes that has Special in the name.
跳过名称中含有“特殊”的课程。

Exclude patterns are applied before the include patterns. If no include or exclude patterns are defined then all the Route classes discovered in the packages will be returned.
排除模式在包含模式之前应用。如果未定义包含或排除模式,则将返回在包中发现的所有路由类。

? matches one character * matches zero or more characters ** matches zero or more segments of a fully qualified name
? 匹配一个字符 * 匹配零个或多个字符 ** 匹配零个或多个完全限定名称的段

Using context scanning 使用上下文扫描

You can allow Camel to scan the container context, e.g. the Spring ApplicationContext for route builder instances. This allow you to use the Spring <component-scan> feature and have Camel pickup any RouteBuilder instances which was created by Spring in its scan process.
您可以允许 Camel 扫描容器上下文,例如 Spring 的 ApplicationContext,以查找路由构建器实例。这样可以使用 Spring 的功能,并让 Camel 获取由 Spring 在扫描过程中创建的任何实例。

<!-- enable Spring @Component scan -->
<context:component-scan base-package="org.apache.camel.spring.issues.contextscan"/>

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <!-- and then let Camel use those @Component scanned route builders -->
    <contextScan/>
</camelContext>

This allows you to just annotate your routes using the Spring @Component and have those routes included by Camel:
这样你就可以使用 Spring 的注解来注释你的路由,并让 Camel 包含这些路由

@Component
public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("direct:start")
            .to("mock:result");
    }
}

You can also use the ANT style for inclusion and exclusion, as mentioned above in the package scan section.
您还可以使用上面在包扫描部分提到的 ANT 样式进行包含和排除。

Additional configuration of Spring XML
Spring XML 的附加配置

See more details at Camel Spring XML Auto Configuration.
请参阅 Camel Spring XML 自动配置的更多详细信息。