Understanding of Spring Spel expressions

Source: Internet
Author: User

Spring's IOC essence is a container, the factory of an object, we register our bean object through a configuration file, and through which he assembles objects and frames.

Spel expression is a string programming, similar to the function of the eval inside JS, through which you can run the string content

Features: A dynamic programming in a configuration file (XML configuration file or annotation expression)--------------------------Mainstream programming is a GUI-based development model (XML development model) for dynamic programming

Important: Spel can be implemented by stitching strings as code, and some flexible functions

<BeanID= "Numberguess"class= "Org.spring.samples.NumberGuess">  < Propertyname= "Randomnumber"value= "#{T (Java.lang.Math). Random () * 100.0}"/>  <!--Other Properties -</Bean><BeanID= "Taxcalculator"class= "Org.spring.samples.TaxCalculator">  < Propertyname= "Defaultlocale"value= "#{systemproperties[' user.region '}"/>  <!--Other Properties -</Bean><BeanID= "Numberguess"class= "Org.spring.samples.NumberGuess">  < Propertyname= "Randomnumber"value= "#{T (Java.lang.Math). Random () * 100.0}"/>  <!--Other Properties -</Bean><BeanID= "Shapeguess"class= "Org.spring.samples.ShapeGuess">  < Propertyname= "Initialshapeseed"value= "#{numberguess.randomnumber}"/>  <!--Other Properties -</Bean>
ExpressionParser parser = new Spelexpressionparser (); inventor Tesla = new inventor ("Nikola Tesla", "Serbian"); Tesla.setplaceofbirth (New Placeofbirth ("Smiljan")); Standardevaluationcontext context = new Standardevaluationcontext (Tesla); String City = parser.parseexpression ("Placeofbirth?. City "). GetValue (context, string.class); System.out.println (city); Smiljantesla.setplaceofbirth (null); city = Parser.parseexpression ("Placeofbirth?. City "). GetValue (context, string.class); System.out.println (city); Null-does not throw NullPointerException!!!

Otherwise: we can only pass Javacompiler compiler = Toolprovider.getsystemjavacompiler (); To compile the string generation class

 Packagecom.test;Importjava.io.IOException;ImportJava.lang.reflect.Method;ImportJava.net.URI;Importjava.util.Arrays;ImportJavax.tools.JavaCompiler;ImportJavax.tools.JavaFileObject;ImportJavax.tools.SimpleJavaFileObject;ImportJavax.tools.StandardJavaFileManager;ImportJavax.tools.ToolProvider; Public classcompilestring { Public Static voidMain (string[] args)throwsException {Javacompiler compiler=Toolprovider.getsystemjavacompiler (); System.out.println (""+Toolprovider.getsystemjavacompiler ()); Standardjavafilemanager FileManager=Compiler.getstandardfilemanager (NULL,NULL,NULL); Stringobject So=NewStringobject ("Calculatortest",                 "Class Calculatortest {" + "public int multiply (int multiplicand, int multiplier) {"                         + "System.out.println (multiplicand);"                         + "System.out.println (multiplier);" + "return multiplicand * multiplier;" + "}" + "}"); Javafileobject file=So ; iterable Files=arrays.aslist (file); Javacompiler.compilationtask Task= Compiler.gettask (NULL, FileManager,NULL,NULL,NULL, files); Boolean result=Task.call ();         SYSTEM.OUT.PRINTLN (result); if(Result) {Class clazz= Class.forName ("Calculatortest"); Object instance=clazz.newinstance (); Method m= Clazz.getmethod ("Multiply",NewClass[] {int.class,                     int.class }); Object[] o=NewObject[] {3, 2 };         System.out.println (M.invoke (instance, O)); }     } }classStringobjectextendsSimplejavafileobject {PrivateString contents =NULL;  PublicStringobject (String className, String contents)throwsException {Super(Uri.create ("string:///" + classname.replace ('. ', '/'))                 +Kind.SOURCE.extension), Kind.source);  This. Contents =contents; }     PublicCharsequence Getcharcontent (Booleanignoreencodingerrors)throwsIOException {returncontents; } }

Understanding of Spring Spel expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.