Spring Expression Language

接着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 == '[email protected]'
			Expression exp5 = parser.parseExpression("email == '[email protected]'");
			boolean msg5 = exp5.getValue(testContext, Boolean.class);
			System.out.println(msg5);
	}
}

猜你喜欢

转载自cywhoyi.iteye.com/blog/1945771