Spring的IOC本质就一个容器,也就是一个对象的工厂,我们通过配置文件注册我们的Bean对象,通过他进行对象的组装与床架。

SpEL表达式就是一种字符串编程,类似于JS里面的EVAL的作用,通过它可以运行字符串内容

特点:算是一种动态的编程在配置文件(xml配置文件或者注解表达式)--------------------------主流的编程都是基于GUI的开发模式(XML开发模式)里面的动态编程

重点:要是通过拼接字符串作为代码运行,SpEL就可以实现,一些灵活的功能

<bean id="numberGuess" class="org.spring.samples.NumberGuess">
<property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>
<!-- other properties -->
</bean>
<bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
<property name="defaultLocale" value="#{ systemProperties['user.region'] }"/>
<!-- other properties -->
</bean>
<bean id="numberGuess" class="org.spring.samples.NumberGuess">
<property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>
<!-- other properties -->
</bean>
<bean id="shapeGuess" class="org.spring.samples.ShapeGuess">
<property name="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); // Smiljan
tesla.setPlaceOfBirth(null);
city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, String.class);
System.out.println(city); // null - does not throw NullPointerException!!!

否则:我们就只能通过JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 来编译字符串生成类

package com.test;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Arrays;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class CompileString {
public static void main(String[] args) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
System.out.println(""+ToolProvider.getSystemJavaCompiler());
StandardJavaFileManager fileManager = compiler.getStandardFileManager(
null, null, null);
StringObject so = new StringObject(
"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", new Class[] { int.class,
int.class });
Object[] o = new Object[] { 3, 2 };
System.out.println(m.invoke(instance, o));
}
}
}
class StringObject extends SimpleJavaFileObject {
private String contents = null;
public StringObject(String className, String contents) throws Exception {
super(URI.create("string:///" + className.replace('.', '/')
+ Kind.SOURCE.extension), Kind.SOURCE);
this.contents = contents;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors)
throws IOException {
return contents;
}
}
这种模式要使用java自定义的类加载器进行加载对应的类,对于类加载器可以参考网上的其他的文章....,通过这样可以实现动态的编程。
本质上,不管是java还是JS代码以及注解,最好的方式就是支持基于字符串的动态编程实现,目前都可以实现...............................

Spring SpEL表达式的理解的更多相关文章

  1. spring spel表达式语言

    一.通过bean的id对bean进行引用 1.对其他bean的引用 <property name="dept" value="#{dept}"/> ...

  2. 【spring boot】SpringBoot初学(2.2)– SpEL表达式读取properties属性到Java对象

    前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置:(注意测试方法在,test下的SpelValueApplicationTest.c ...

  3. Spring实战学习笔记之SpEL表达式

            在Spring XML配置文件中装配Bean的属性和构造参数都是静态的,而在运行期才知道装配的值,就可以使用SpEL实现         SpEL表达式的首要目标是通过计算获得某个值. ...

  4. Spring Security -SpEL表达式

    Spring Security -SpEL表达式 开启SpEL表达式 <!-- use-expressions是否开启 SpEL表达式 o.s.s.web.access.expression.W ...

  5. Spring学习-- SpEL表达式

    Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于 EL:SpEL 使用 #{...} 作为定界符 , 所有在大括号中的字符都将被认为是 SpE ...

  6. Spring系列.SpEL表达式

    Spring表达式语言 SpEL语言是一种强大的表达式语言,支持在运行时查询和操作对象.SpEL表达式不一定要创建IOC容器后才能使用.用户完全可以单独调用SpEL的API来独立的使用时SpEL表达式 ...

  7. Spring 缓存注解 SpEL 表达式解析

    缓存注解上 key.condition.unless 等 SpEL 表达式的解析 SpEl 支持的计算变量: 1)#ai.#pi.#命名参数[i 表示参数下标,从 0 开始] 2)#result:Ca ...

  8. Spring讲解-----------表达式语言

    转自:https://blog.csdn.net/u011225629/article/details/47143083 5.1  概述5.1.1  概述       Spring表达式语言全称为“S ...

  9. SpringBoot SpEL表达式注入漏洞-分析与复现

    目录 0x00前言 0x01触发原因 0x02调试分析 0x03补丁分析 0x04参考文章 影响版本: 1.1.0-1.1.12 1.2.0-1.2.7 1.3.0 修复方案:升至1.3.1或以上版本 ...

随机推荐

  1. Ruby Numeric

    Numeric |-- Float |-- Integer |-- Fixnum |-- Bignum Numeric的基本结构 整数的差异,一般的数字Fixnum就能够处理,即使超过了Fixnum的 ...

  2. Saltstack系列3:Saltstack常用模块及API

    说明 salt '*' sys.list_modules #列出当前版本支持的模块 API原理是通过调用master client模块,实例化一个LocalClient对象,再调用cmd()方法来实现 ...

  3. metaspolit教程

    网上的安装方式都是抄来抄去,我也抄了下,不过好歹自己试了下,有所不同 git clone https://github.com/rapid7/metasploit-framework.git vim ...

  4. M2的来源很简单

     http://bbs.tianya.cn/post-develop-412703-1.shtml M2的来源很简单,一部分是外汇占款,这个是我们国家的缺陷,如果外汇不管制,其实是不会发生增长人民币的 ...

  5. PHP注释有意思的排列

    <?php // // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // O\ = /O // ____/`---'\___ ...

  6. Hadoop学习1--解决启动过程中的问题

    方法:http://www.aboutyun.com/thread-12694-1-1.html http://www.linuxidc.com/topicnews.aspx?tid=13 http: ...

  7. POJ 1740 A New Stone Game(普通博弈)

    A New Stone Game 题意: 对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分给其它的某些堆.最后谁无子可取即输 ...

  8. tcpdump学习

    #直接启动tcpdump将监视第一个网络接口上所有流过的数据包 -n不解析地址到nametcpdump -n #监视指定网络接口的数据包,不指定则为 eth0tcpdump -i eth1 #监视指定 ...

  9. bug_ _ java.lang.IllegalArgumentException: pointerIndex out of range 问题的两种解决办法

    ========== 4     如何解决java.lang.IllegalArgumentException: pointerIndex out of range? 今天遇到一个bug:java.l ...

  10. 转载__UI之Frgment

    http://www.cnblogs.com/plokmju/p/3239265.html 前言 开门见山开篇名义,本篇博客将讲解一下Android中Fragment的内容,必要的地方会提供相应的演示 ...