I. Introduction of SPEL
The spring expression language is all called "Spring expression Language" and is abbreviated as "Spel" to build complex expressions at run-time
Steps to use:
1) Create parser: ExpressionParser interface represents parser, Spelexpressionparser provides default implementation
2) Parse expression: Use the Expressionparser.parseexpression () method to parse an expression into an expressions object
3) Construct context: Used to define variables, represented by Evaluationcontext interfaces, Standardevaluationcontext provides default implementations
4) Evaluation: Use the Expression.getvalue () method to evaluate the value of an expression based on the context
@Test Public voidTestspel () {//Spel ParserExpressionParser parser =NewSpelexpressionparser (); //spel StatementsExpression expression1 = parser.parseexpression ("895"); Assert.asserttrue (895 = = Expression1.getvalue (int.class)); //spel StatementsExpression expression2 = parser.parseexpression ("' Hello '"); Assert.asserttrue (Stringutils.equals ("Hello", Expression2.getvalue (String.class))); //Spel ContextEvaluationcontext context =NewStandardevaluationcontext (); Context.setvariable ("End", "ZZ"); Expression Expression3= Parser.parseexpression (' yy ' + ' pp '). Concat (#end) "); Assert.asserttrue (Stringutils.equals ("Yyppzz", expression3.getvalue (context, String.class)));}
Second, use spel in the bean definition
ApplicationContext implements the default support for Spel, which is injected with the bean definition, using the "#{spel expression}" representation
1) XML mode
<BeanID= "STR1"class= "Java.lang.String"> <Constructor-argIndex= "0"value= "Uouu"/> </Bean><BeanID= "Propbean"class= "Java.lang.String"> <Constructor-argIndex= "0"value= "#{str1}"/></Bean>
@Test Public void TestSpel2 () { new classpathxmlapplicationcontext ("Spring-context.xml"); = Context.getbean ("Propbean", String. Class); Assert.asserttrue (Stringutils.equals ("Uouu", prop));}
2) Annotation Method
public class Hello {@Value ( "#{str1}" private String password; public String GetPassword () { return password; }}
<!-- define bean --> < bean class = "Cn.matt.spel.Hello" ></ bean > <!-- opening annotations --> < context:annotation-config />
@Test Public void TestSpel3 () { new classpathxmlapplicationcontext ("Spring-context.xml"); = Context.getbean (Hello. Class); Assert.asserttrue (Stringutils.equals ("Uouu", Hello.getpassword ()));
Third, the use of property files
How the Spring properties file is configured:
<!--Full write mode -<BeanID= "Propertyplaceholderconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> < Propertyname= "Locations"> <List> <value>Jdbc1.properties</value> <value>Jdbc2.properties</value> </List> </ Property></Bean>
Shorthand for the above configuration:
<!---<location= "Classpath:jdbc1.properties, Classpath:jdbc2.properties "/>
Examples of usage are as follows:
# jdbc1.properties File username=rootpassword=root
# jdbc2.properties File username=adminpassword=admin
< bean id = "Propbean" class = "java.lang.String" > < constructor-arg index = "0" value = "${password}" /> </ bean >
@Test Public void TestSpel2 () { new classpathxmlapplicationcontext ("Spring-context.xml"); = Context.getbean ("Propbean", String. Class); Assert.asserttrue (Stringutils.equals ("admin", prop));}
Note:
1) Spring uses "${property name}" to represent property values, unlike spel using "#{spel expression}"
2) When the same attribute value exists in multiple property files, the latter overrides the former
3) Support annotation mode, using the same way as spel annotation, such as: @Value ("${password}")
Reference:
Fifth 5.1 Overview of Spring Expression Language 5.2 spel Foundation--Follow me to learn Spring3
The fifth Chapter 5.3 Spel Grammar of Spring Expression language--learn from me Spring3
Fifth Spring expression Language 5.4 using el-in the bean definition learn from me spring3
Spring <context:property-placeholder> Instructions for use
Import multiple properties files using <context:property-placeholder> tags
Introduction to Spring Use (iv)--spel