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. 【性能诊断】四、单功能场景的性能分析(RedGate,找到同一个客户端的并发请求被串行化问题)

    问题描述: 客户端js连续发起两个异步http请求,请求地址相同,但参数不同:POST http://*.*.*.*/*****/webservice/RESTFulWebService/RESTFu ...

  2. Android.mk文件语法规范 原文

    序言:------------- 此文档旨在描述Android.mk文件的语法,Android.mk文件为Android NDK(原生开发)描述了你C/C++源文件.为了明白下面的内容,你必须已经阅读 ...

  3. mybatis获得刚刚插入的自增的值

    转自这里 在http://blog.csdn.net/zhangwenan2010/article/details/7579191   介绍了MyBatis 3 的配置过程, 其中,Product 类 ...

  4. phpstorm laravel单元测试 配置

    laravel中集成了单元测试工具phpunit可以在项目的根目录下进行使用,命令是:phpunti ./tests/单元测试文件名称.在phpstorm中使用phpunit需要做一些配置,指定com ...

  5. DDD学习笔记一

    由于也是初学DDD,难免有很多不足和认识错误的地方.多数内容来自其他网络资料或者书籍. 参考:http://www.cnblogs.com/Leo_wl/p/4418663.html 希望多多提出宝贵 ...

  6. 战胜忧虑<1>——不要让忧郁侵入你的生活

    1.不要让忧郁侵入你的生活. 备注:忧郁:一种情绪与心理状态,指一个人呈现哀伤.心情低落的状况,绝望与沮丧为其特色. 解决方法:奥斯勒博士说的那样:用铁门把过去和未来隔断,生活在完全独立的今天. 现在 ...

  7. 编译在arm板上使用的sqlite3的静动态库

    采用的是sqlite-autoconf-3080002.tar.gz 解压 tar xvf sqlite-autoconf-3080002.tar.gz 进入 cd sqlite-autoconf-3 ...

  8. Oracle-decode函数

    decode函数 简单例子:管理员登录Oracle select sid, username, decode(command, 0, 'None', 2, 'Insert', 3, 'Select', ...

  9. (C++) 基本面试题(整理)

    1.new.delete.malloc.free关系 new/delete是C++的运算符.new 调用构造函数用于动态申请内存,delete调用对象的析构函数,用于释放内存. malloc与free ...

  10. linux下sublime配置c++11编译环境

    Tools->Build System->New Build System { "cmd": ["g++", "-std=c++11&qu ...