315.3. 샘플
315.3.1. 표현식 템플릿
표현식 템플릿 활성화가 가능하므로 #{
}
구분 기호가 SPEL 표현식을 사용해야 합니다. 이를 통해 SpEL 표현식을 일반 텍스트와 결합하고 매우 간단한 템플릿 언어로 사용할 수 있습니다.
예를 들어 다음 경로를 구성하는 경우:
from("direct:example") .setBody(spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}")) .to("mock:result");
위의 경로에서 notice spel은 spel을 setBody
메서드의 매개 변수로 전달된 Expression로 사용하기 때문에 org.apache.camel.spel
.spel.spel.spel 에서 가져와야 하는 정적 메서드입니다. fluent API를 사용하는 경우 다음 작업을 수행할 수 있습니다.
from("direct:example") .setBody().spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}") .to("mock:result");
이제 setBody()
메서드의 spel
메서드를 사용합니다. 그리고 이것은 org.apache.camel.spel.spel.spel.spel
으로부터 spel 메서드를 정적으로 가져올 필요가 없습니다.
그리고 본문에 "ECDSA" 문자열이 포함된 메시지를 보내고 "dayOrNight" 값이 "dayOrNight"인 헤더를 전송했습니다.
template.sendBodyAndHeader("direct:example", "World", "dayOrNight", "day");
mock:result
의 출력은 "Hello World가 됩니다. <아름다운 날>
315.3.2. Bean 통합
SpEL 표현식의 Registry (대부분 ApplicationContext
)에 정의된Bean을 참조할 수 있습니다. 예를 들어 ApplicationContext
에 "foo"라는Bean이 있는 경우 다음과 같이 "bar"에서 "bar" 메서드를 호출할 수 있습니다.
#{@foo.bar == 'xyz'}
315.3.3. 엔터프라이즈 통합 패턴의 SPEL
SpEL을 Recipient List 의 표현식으로 사용하거나 Message Filter 내에서 서술자로 사용할 수 있습니다.
<route> <from uri="direct:foo"/> <filter> <spel>#{request.headers['foo'] == 'bar'}</spel> <to uri="direct:bar"/> </filter> </route>
Java DSL과 동일합니다.
from("direct:foo") .filter().spel("#{request.headers['foo'] == 'bar'}") .to("direct:bar");