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

<?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.xsd
		http://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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值