Java Reference
In-Depth Information
@Value("#{ emailUtilities.password }")
private String password;
private String host;
You can also use the expression language to inject references to other named beans in the same
@Value("#{ emailUtilities }")
private EmailUtilities emailUtilities ;
In this case, because there's only one bean in the context with the interface EmailUtilities , you
could also do this:
private EmailUtilities emailUtilities ;
Although there are other mechanisms for discriminating against beans of the same interface, the
expression language becomes very handy here because it lets you simply discriminate by bean id .
You can use the expression language in your XML configurations in exactly the same way as with the
annotation support. Even the prefix #{ and the suffix } are the same.
<bean class="com.apress.springenterpriserecipes.spring3.
p:randomNumber="#{ T(java.lang.Math).random() * 100.0 }"
Using the Spring Expression Language Parser
The SpEL is used primarily inside the XML configuration and annotation support
provided with the Spring framework, but you're free to use the expression language.
The centerpiece of the functionality is provided by the expression parser,
org.springframework.expression.spel.antlr.SpelAntlrExpressionParser , which
you can instantiate directly:
ExpressionParser parser = new SpelAntlrExpressionParser();
Conceivably, you could build an implementation that complies with the ExpressionParser interface
and builds your own integration using this API. This interface is central for evaluating expressions
written using the SpEL. The simplest evaluation might look like this:
Expression exp = parser.parseExpression("'ceci n''est pas une String'" );
String val = exp.getValue(String.class);
Here, you evaluate the String literal (notice that you're escaping the single quote with another
single quote, not with a backslash) and return the result. The call to getValue() is generic, based on the
type of the parameter, so you don't need to cast.
Search WWH ::

Custom Search