Java Reference
In-Depth Information
Type
Use
Example
Collection selection
Selection lets you dynamically
filter objects from a collection
or map by evaluating a predicate
on each item in the collection
and keeping only those elements
for which the predicate is true.
In this case, you evaluate the
java.util.Map.Entry.value property
for each Entry in the Map and if
the value (in this case a String ),
lowercased, starts with s , then it is
kept. Everything else is discarded.
mapOfStatesAndCapitals.?[value.toLow
erCase().startsWith('s')]
Templated
expression
You can use the expression language
to evaluate expressions inside of
string expressions. The result is
returned. In this case, the result is
dynamically created by evaluating
the ternary expression and including
'good' or 'bad' based on the result.
Your fortune is
${T(java.lang.Math).random()> .5 ?
'good' : 'bad'}
Uses of the Language in Your Configurations
The expression language is available via XML or annotations. The expressions are evaluated at creation
time for the bean, not at the initialization of the context. This has the effect that beans created in a
custom scope are not configured until the bean is in the appropriate scope. You can use them in the
same way via XML or annotations.
The first example is the injection of a named expression language variable, systemProperties , which is
just a special variable for the java.util.Properties instance that's available from System.getProperties() .
The next example shows the injection of a system property itself directly into a String variable:
@Value("#{ systemProperties }")
private Properties systemProperties;
@Value("#{ systemProperties['user.region'] }")
private String userRegion;
You can also inject the result of computations or method invocations. Here you're injecting the
value of a computation directly into a variable:
@Value("#{ T(java.lang.Math).random() * 100.0 }")
private double randomNumber;
The next examples assume that another bean is configured in the context with the name
emailUtilities . The bean in turn has JavaBean-style properties that are injected into the following fields:
@Value("#{ emailUtilities.email }")
private String email;
Search WWH ::




Custom Search