Spring学习之SpEL(一)

来源:互联网 发布:软件开发思路 编辑:程序博客网 时间:2024/04/29 16:02

整体代码结构:


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;}@Overridepublic 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();}@Overridepublic 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

<?xml version="1.0" encoding="UTF-8"?><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:util="http://www.springframework.org/schema/util"xmlns:c="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"><!-- util:properties加载指定的资源文件 --><util:properties id="conf" location="classpath:context.properties"></util:properties><!-- 使用表达式传递参数 --><!-- 使用属性文件加载country --><bean id="chinese" class="com.kinsey.woo.dto.Chinese"p:age="#{T(java.lang.Math).random() * 100}"p:country="#{conf.country}"p:person="#{new  com.kinsey.woo.dto.Person('z3')}"></bean></beans>

context.propertis

#国家country=China

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





0 0
原创粉丝点击