接着http://ray-yui.iteye.com/blog/1944582的火热,兄弟也来一把,关于
Spring Expression Language
主要是告诉大家如何通过annotion以及XML的方式进行EL表达方式的解析,同样也会告诉大家如何通过ExpressionParser
接口类实现对于EL表达式的解析。
工程采用标准的MAVEN,在附件中有提供代码
接下来的例子是把Book类以及其它成员变量注入到Author类
package com.javacodegeeks.snippets.enterprise; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("authorBean") public class Author { @Value("沈志华") private String name; @Value("#{bookBean}") private Book book; @Value("#{bookBean.title}") private String bookTitle; @Value("#{bookBean.getBookInfo('沈志华')}") private String fullAuthorInfo; public String getName() { return name; } public void setName(String name) { this.name = name; } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public String getBookTitle() { return bookTitle; } public void setBookTitle(String bookTitle) { this.bookTitle = bookTitle; } public String getFullAuthorInfo() { return fullAuthorInfo; } public void setFullAuthorInfo(String fullAuthorInfo) { this.fullAuthorInfo = fullAuthorInfo; } @Override public String toString(){ return name + " has writen the book : " + book + ". \n" + bookTitle + " is a wonderful title of a wonderful book."; } }
package com.javacodegeeks.snippets.enterprise; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("bookBean") public class Book { @Value("12345") private long id; @Value("朝鲜战争揭秘") private String title; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getBookInfo(String authorName){ return authorName + " has writen the book " + title + ", with book id " + ""+ id + "."; } @Override public String toString(){ return title; } }
在使用annotion配置文件需要声明
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <context:component-scan base-package="com.javacodegeeks.snippets.enterprise" /> </beans>
当然之前博主已经讲了很多EL表达式使用,比较全集
那么如果我们自己也要造轮子,为何不采用ExpressionParser的API,接下来就演示下具体的操作过程
package com.javacodegeeks.snippets.enterprise; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; public class ExpressionParserApp { public static void main(String[] args) { ExpressionParser parser = new SpelExpressionParser(); //literal expressions Expression exp = parser.parseExpression("'Hello World'"); String msg1 = exp.getValue(String.class); System.out.println(msg1); //method invocation Expression exp2 = parser.parseExpression("'Hello World'.length()"); int msg2 = (Integer) exp2.getValue(); System.out.println(msg2); //Mathematical operators Expression exp3 = parser.parseExpression("100 * 2"); int msg3 = (Integer) exp3.getValue(); System.out.println(msg3); //create an test object Test test = new Test(); //test EL with test object StandardEvaluationContext testContext = new StandardEvaluationContext(test); //display the value of test.email property Expression exp4 = parser.parseExpression("email"); String msg4 = exp4.getValue(testContext, String.class); System.out.println(msg4); //test if test.email == 'Hello@javacodegeeks.com' Expression exp5 = parser.parseExpression("email == 'Hello@javacodegeeks.com'"); boolean msg5 = exp5.getValue(testContext, Boolean.class); System.out.println(msg5); } }
相关推荐
NULL 博文链接:https://orange5458.iteye.com/blog/1549551
Spring 3.0中,新的表达式语言名叫Spring Expression Language(SpEL,直译就是Spring表达式语言)。Shaun将其描述为“嵌入在Spring编程模型上的,可以用来在Spring项目之间描述任务以及配置的微型脚本语言。SpEL...
Spring 3.0中,新的表达式语言名叫Spring Expression Language(SpEL,直译就是Spring表达式语言)。Shaun将其描述为“嵌入在Spring编程模型上的,可以用来在Spring项目之间描述任务以及配置的微型脚本语言。SpEL...
它与Spring框架紧密集成,并提供了对Spring Expression Language(SpEL)的支持。Spring Boot Thymeleaf提供了一个便捷的方式来在Spring Boot应用程序中使用Thymeleaf模板引擎,简化了开发过程,并提供了丰富的功能...
* * * * Core Container(核心容器) Spring的核心容器是其他模块建立的基础,它主要由Beans模块、Core模块、Context模块、Context-support模块和SpEL(Spring Expression Language,Spring表达式语言)模块组成,...
Spring Expression Language(简称 SpEL)是一个支持查询和操作运行时对象导航图功能的强大的表达式语言,它的语法类似于传统 EL(如jsp中的EL表达式),但提供额外的功能,最出色的就是函数调用和简单字符串的模板...
Spring Expression Language(简称SpEL)是一种功能强大的表达式语言、 在Spring3中就已经支持EL表达式了, Spring Expression Language(SpEL)是类似于OGNL和JSF EL的表达式语言, 能够在运行时构建复杂表达式, 存取...
- SpEL (Spring Expression Language) - Caching using Spring's cache abstraction - Sending and receiving JMS messages using Spring - Aspect-oriented programming support in Spring - Sending emails using ...
Spring Expression Language (SpEL)中文文档。基于Spring4.x。
thymeLea支持Spring Expression Language语言作为方言,也就是SpEL,在学习JSP时我们对EL表达式都有一定的认识了,SpEL是可以用于Spring中的一种EL表达式。简而言之,与我们使用过的JSP,需要的童鞋赶紧下载吧。
Explore the power of Beans using Dependency Injection, wiring, and Spring Expression Language Implement and integrate a persistent layer in your application and also integrate an ORM such as Hibernate...
3.5.2. Wiring with the Spring Expression Language 3.6. Summary Chapter 4. Aspect-oriented Spring 4.1. What is aspect-oriented programming? 4.1.1. Defining AOP terminology 4.1.2. Spring’s AOP support ...
本次发布悠 80 多处小问题,包含对 Spring Expression Language (SpEL), annotation 支持和嵌入式数据库一些小的改进。详细情况请参考变更日志。 不要忘记 Spring 用户可以在 社区论坛 提出问题,并可以在 JIRA ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
java8 源码 Spring 版本 Version Feature Spring 2.5 发布于 2007 年。...这是第一个支持注解的版本。...Spring ...Spring ...轻量级:Spring ...容器:Spring ...Spring ...异常:Spring ...Expression Language) Spring Context A
Converters and Formatters Chapter 7: Validators Chapter 8: The Expression Language Chapter 9: JSTL Chapter 10: Internationalization Chapter 11: File Upload Chapter 12: File Download Chapter 13: ...
OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,...