此内容没有您所选择的语言版本。

2.5. Aspect Oriented Programming


Overview

The aspect oriented programming (AOP) feature in Apache Camel enables you to apply before and after processing to a specified portion of a route. As a matter of fact, AOP does not provide anything that you could not do with the regular route syntax. The advantage of the AOP syntax, however, is that it enables you to specify before and after processing at a single point in the route. In some cases, this gives a more readable syntax. The typical use case for AOP is the application of a symmetrical pair of operations before and after a route fragment is processed. For example, typical pairs of operations that you might want to apply using AOP are: encrypt and decrypt; begin transaction and commit transaction; allocate resources and deallocate resources; and so on.

Java DSL example

In Java DSL, the route fragment to which you apply before and after processing is bracketed between aop() and end(). For example, the following route performs AOP processing around the route fragment that calls the bean methods:
from("jms:queue:inbox")
   .aop().around("log:before", "log:after")
       .to("bean:order?method=validate")
       .to("bean:order?method=handle")
   .end()
   .to("jms:queue:order");
Copy to Clipboard Toggle word wrap
Where the around() subclause specifies an endpoint, log:before, where the exchange is routed before processing the route fragment and an endpoint, log:after, where the exchange is routed after processing the route fragment.

AOP options in the Java DSL

Starting an AOP block with aop().around() is probably the most common use case, but the AOP block supports other subclauses, as follows:
  • around()—specifies before and after endpoints.
  • begin()—specifies before endpoint only.
  • after()—specifies after endpoint only.
  • aroundFinally()—specifies a before endpoint, and an after endpoint that is always called, even when an exception occurs in the enclosed route fragment.
  • afterFinally()—specifies an after endpoint that is always called, even when an exception occurs in the enclosed route fragment.

Spring XML example

In the XML DSL, the route fragment to which you apply before and after processing is enclosed in the aop element. For example, the following Spring XML route performs AOP processing around the route fragment that calls the bean methods:
<route>
    <from uri="jms:queue:inbox"/>
    <aop beforeUri="log:before" afterUri="log:after">
        <to uri="bean:order?method=validate"/>
        <to uri="bean:order?method=handle"/>
    </aop>
    <to uri="jms:queue:order"/>
</route>
Copy to Clipboard Toggle word wrap
Where the beforeUri attribute specifies the endpoint where the exchange is routed before processing the route fragment, and the afterUri attribute specifies the endpoint where the exchange is routed after processing the route fragment.

AOP options in the Spring XML

The aop element supports the following optional attributes:
  • beforeUri
  • afterUri
  • afterFinallyUri
The various use cases described for the Java DSL can be obtained in Spring XML using the appropriate combinations of these attributes. For example, the aroundFinally() Java DSL subclause is equivalent to the combination of beforeUri and afterFinallyUri in Spring XML.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat