Spring学习之SpEL(一)

整体代码结构:


Person.java

package com.kinsey.woo.dto;

public class Person {
	
	private String name;

	public Person() {
		super();
	}
	public Person(String name) {
		this.name=name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return "[name:" + name + "]";
	}


}

Chinese.java

package com.kinsey.woo.dto;

public class Chinese{
	
	private Person person; 
	
	private String country;
	
	private Integer age;
	
	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public Chinese() {
		super();
	}
	
	@Override
	public String toString() {
		return "[" + "country:" + country + ",age:" + age
				+ person + "]";
	}
	
}

RunMain.java

package com.kinsey.woo.main;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import com.kinsey.woo.dto.Chinese;

public class RunMain {

	public static void main(String[] args) throws Exception {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-conf.xml");
		Chinese chinese = ctx.getBean("chinese", Chinese.class);
		System.out.println("chinese" + chinese);
		
		ExpressionParser parser = new SpelExpressionParser();
		//直接量表达式
		Expression exp = (Expression) parser.parseExpression("'Hello World!'");
		System.out.println(exp.getValue());
		
		//在表达式中创建数组
		exp = (Expression) parser.parseExpression("new String[]{'java','python','c++'}");
		System.out.println(exp.getValue());
		
		//类型运算符
		System.out.println(parser.parseExpression("T(java.lang.Math).random()").getValue());
		System.out.println(parser.parseExpression("T(System).getProperty('os.name')").getValue());
		
		//调用构造器
		System.out.println(parser.parseExpression("new String('Hello World').substring(1,7)").getValue());
		
	}

}

beans-conf.xml




	
	
	
	
	
	
	
	
	




context.propertis

#国家
country=China

SpEL项目中用的少,后续补充这一块知识点





你可能感兴趣的:(Spring)