当前位置:首页 > 开发 > 互联网 > 正文

Spring Expression Language

发表于: 2013-09-23   作者:cywhoyi   来源:转载   浏览次数:
摘要: 接着http://ray-yui.iteye.com/blog/1944582的火热,兄弟也来一把,关于 Spring Expression Language   主要是告诉大家如何通过annotion以及XML的方式进行EL表达方式的解析,同样也会告诉大家如何通过ExpressionParser 接口类实现对于EL表达式的解析。 工程采用标准的MAVEN,在附件中有提

接着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);
	}
}

 

Spring Expression Language

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
robot and nature,becoming one? What's their aim.. and the like.. re-brained new machine era i
pdf("first.pdf") plot(wt,disp) dev.off() 将图像输出到pdf文件 getwd(); [1] "D:/Personal/Docume
come from:http://deerchao.net/tutorials/regex/regex.htm 首页 | 常用正则表达式 | 正则表达式测
A Neural Probabilistic Language Model,这篇论文是Begio等人在2003年发表的,可以说是词表示的鼻
UML的组成 Unified Modeling Language (UML)又称统一建模语言或标准建模语言,是始于1997年一个OMG
What Is an Object? Objects are key to understanding object-oriented technology. Look around r
点击这里下载chm part1 点击这里下载pdf 点击这里下载chm part2 提示:chm版 【内容简介】 本书详细
If a programming language was a boat… Hi, Im wondering how i can create a boat in turing and
Yahoo! Query Language - YDN Yahoo! Query Language What is YQL? The Yahoo! Query Language is a
Skip to main content Sign in (or register) English Technical topics <li style="font-family
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号