Spring Expression Language (SpEL)
2012-11-05 14:15
337 查看
http://camel.apache.org/spel.html
Camel allows
SpEL to be used as an
Expression or
Predicate in the
DSL or
Xml Configuration.
expression templating is enabled. This allows you to combine SpEL expressions with regular text and use this as extremely lightweight template language.
For example if you construct the following route:
And sent a message with the string "World" in the body, and a header "dayOrNight" with value "day":
The output on mock:result will be "Hello World! What a beautiful day"
Registry (most likely an ApplicationContext) in your SpEL expressions. For example if you have a bean named "foo" in your
ApplicationContext you can invoke the "bar" method on this bean like this:
Recipient List or as a predicate inside a
Message Filter:
And the equivalent in Java DSL:
pom.xml:
Spring Expression Language(SpEL)
Available as of Camel 2.7Camel allows
SpEL to be used as an
Expression or
Predicate in the
DSL or
Xml Configuration.
Variables
The following variables are available in expressions and predicates written in SpEL:Variable | Type | Description |
---|---|---|
this | Exchange | the Exchange is the root object |
exchange | Exchange | the Exchange object |
exception | Throwable | the Exchange exception (if any) |
exchangeId | String | the exchange id |
fault | Message | the Fault message (if any) |
request | Message | the exchange.in message |
response | Message | the exchange.out message (if any) |
properties | Map | the exchange properties |
property(name) | Object | the property by the given name |
property(name, type) | Type | the property by the given name as the given type |
Samples
Expression templating
SpEL expressions need to be surrounded by #{ } delimiters sinceexpression templating is enabled. This allows you to combine SpEL expressions with regular text and use this as extremely lightweight template language.
For example if you construct the following route:
from("direct:example").setBody(spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}")).to("mock:result");
And sent a message with the string "World" in the body, and a header "dayOrNight" with value "day":
template.sendBodyAndHeader("direct:example", "World", "dayOrNight", "day");
The output on mock:result will be "Hello World! What a beautiful day"
Bean integration
You can reference beans defined in theRegistry (most likely an ApplicationContext) in your SpEL expressions. For example if you have a bean named "foo" in your
ApplicationContext you can invoke the "bar" method on this bean like this:
#{@foo.bar == 'xyz'}
SpEL in enterprise integration patterns
You can use SpEL as an expression forRecipient List or as a predicate inside a
Message Filter:
<route> <from uri="direct:foo"/> <filter> <spel>#{request.headers['foo'] == 'bar'}</spel> <to uri="direct:bar"/> </filter> </route>
And the equivalent in Java DSL:
from("direct:foo").filter().spel("#{request.headers['foo'] == 'bar'}").to("direct:bar");
Dependencies
You need Spring 3.0 or higher to use Spring Expression Language. If you use Maven you could just add the following to yourpom.xml:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring</artifactId> <version>xxx</version> <!-- use the same version as your Camel core version --> </dependency>
相关文章推荐
- Spring in Action 3 -Spring Expression Language (SpEL)
- SPEL语言-Spring Expression Language
- Spring Expression Language (SpEL)-Spring表达式语言
- Spring笔记 - Spring Expression Language (SpEL表达式)
- Spring Expression Language(SpEL) spring表达式语言
- java.lang.NoSuchMethodError: org.springframework.expression.spel.SpelParserConfiguration
- How to create custom methods for use in spring security expression language annotations
- org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method service() cannot be found on com.my.blog.springboot.thymeleaf.util.MethodTest type
- org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method test() on null context object
- Spring学习总结(四)——表达式语言 Spring Expression Language
- spring security 中异常 org.springframework.expression.spel.SpelEvaluationException
- Spring Expression Language 全面介绍
- Spring学习总结(四)——表达式语言 Spring Expression Language
- Spring学习总结(四)——表达式语言 Spring Expression Language
- org.springframework.expression.spel.SpelEvaluationException: EL1005E:(pos 0): Type cannot be found
- Spring Expression Language (Spring 3.0)
- org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'productCode
- Spring Cron Expression
- Spring 之SPEL表达式
- spring aop expression 匹配多个条件 多个表达式