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