the El expression of spring
Spel Introduction
El expressions are already supported in Spring3, and Spring Expression Language (spel) is an expression language similar to Ognl and JSF El, capable of building complex expressions at run time, accessing object properties, invoking object methods, and so on. And all Spel support the XML and annotation two ways, using the format: #{spel expression}. Examples that are used in XML are:
<bean id= "Hellobean" class= "com.***". Hello ">
<property name=" item "value=" #{otherbean} "/> <!--inject Otherbean into the Item property of Hellobean- >
<property name= "ItemName" value= "#{otherbean.name}"/> <!-- Inject Otherbean's name into Hellobean's ItemName properties-->
</bean>
Examples to use in annotation:
@Component public
class Test {
@Value ("#{' Tom toUpperCase ()}")
private String name;
As can be seen from the example above, we have unknowingly used the SPEL expression when using spring. In addition to our own use of spel expressions, there are roughly three steps:
1. Build parser
ExpressionParser parser = new Spelexpressionparser ();
2. Analytic expression
Expression exp = parser.parseexpression (spel);
3. Obtain the result
exp.getvalue ();
This is the basic way to use Spel, and there are a lot of features, and here's an example of a text expression
Text Expression support: strings (which need to be declared in single quotes), dates, numbers, Boolean types, and null, which support negative, exponential, and decimal numbers, by default the real numbers use double.parsedouble () for expression type conversions.
Parser.parseexpression ("' Hello '"). GetValue (String.class); Hello, note the single quotation mark
parser.parseexpression ("1.024E+3"). GetValue (Long.class); 1024 , exponential form
parser.parseexpression ("0xFFFF"). GetValue (Integer.class);//65535, Hex
Parser.parseexpression ("true"). GetValue (Boolean.class); True
parser.parseexpression ("null"). GetValue (); Null
variable
A variable can be set to the context through the Standardevaluationcontext SetVariable method, which can be used by the # variable name;
Alternatively, you can create objects directly using the constructor method.
Define variable
String name = "Tom";
Evaluationcontext context = new Standardevaluationcontext (); The context of an expression,
context.setvariable ("MyName", name); In order for an expression to access the object, first place the object in the context
expressionparser parser = new Spelexpressionparser ();
Access variable
parser.parseexpression ("#myName"). GetValue (context, string.class); Tom,
Create object
parser.parseexpression ("New String (' AAA ')") using the variable//direct use of the construction method. GetValue (String.class); Aaa
property and method callsProperty can be used directly by using the property name, which can be case-sensitive (only the first letter is case-insensitive); Arrays, lists can be accessed directly through the following table Form (List[index]); The map can be accessed directly as an index (Map[key]); Method can be accessed directly;
Prepare work person person = new Person ("Tom", 18);
A common Pojo list<string> List = lists.newarraylist ("A", "B");
map<string, string> map = Maps.newhashmap ();
Map.put ("A", "1");
Map.put ("B", "2"); Evaluationcontext context = new Standardevaluationcontext (); The context of an expression, context.setvariable ("person", person);
To allow an expression to access the object, first place the object in the context context.setvariable ("map", map);
Context.setvariable ("list", list);
ExpressionParser parser = new Spelexpressionparser (); Property Parser.parseexpression ("#person. Name"). GetValue (context, string.class); Tom, attribute Access Parser.parseexpression ("#person. Name "). GetValue (context, string.class); Tom, attribute access, but the first letter is capitalized//list parser.parseexpression ("#list [0]"). GetValue (context, String.class)//A, subscript//MA P Parser.parseexpression ("#map [A]"). GetValue (context, string.class); 1, key//method parser.