Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 31. SpEL
Overview 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
The Spring Expression Language (SpEL) is an object graph navigation language provided with Spring 3, which can be used to construct predicates and expressions in a route. A notable feature of SpEL is the ease with which you can access beans from the registry.
Syntax 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
The SpEL expressions must use the placeholder syntax,
#{SpelExpression}
, so that they can be embedded in a plain text string (in other words, SpEL has expression templating enabled).
SpEL can also look up beans in the registry (typically, the Spring registry), using the
@BeanID
syntax. For example, given a bean with the ID, headerUtils
, and the method, count()
(which counts the number of headers on the current message), you could use the headerUtils
bean in an SpEL predicate, as follows:
#{@headerUtils.count > 4}
#{@headerUtils.count > 4}
Adding SpEL package 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
To use SpEL in your routes you need to add a dependency on
camel-spring
to your project as shown in Example 31.1, “Adding the camel-spring dependency”.
Example 31.1. Adding the camel-spring dependency
Variables 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
Table 31.1, “SpEL variables” lists the variables that are accessible when using SpEL.
Variable | Type | Description |
---|---|---|
this | Exchange | The current exchange is the root object. |
exchange | Exchange | The current exchange. |
exchangeId | String | The current exchange's ID. |
exception | Throwable | The exchange exception (if any). |
fault | Message | The fault message (if any). |
request | Message | The exchange's In message. |
response | Message | The exchange's Out message (if any). |
properties | Map | The exchange properties. |
property(Name) | Object | The exchange property keyed by Name. |
property(Name, Type) | Type | The exchange property keyed by Name, converted to the type, Type. |
XML example 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
For example, to select only those messages whose
Country
header has the value USA
, you can use the following SpEL expression:
Java example 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
You can define the same route in the Java DSL, as follows:
from("SourceURL") .filter().spel("#{request.headers['Country'] == 'USA'}") .to("TargetURL");
from("SourceURL")
.filter().spel("#{request.headers['Country'] == 'USA'}")
.to("TargetURL");
The following example shows how to embed SpEL expressions within a plain text string:
from("SourceURL") .setBody(spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}")) .to("TargetURL");
from("SourceURL")
.setBody(spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}"))
.to("TargetURL");