Spirng SpEL xml的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tangyaliang11/article/details/79259999

以下代码保证可以允许,演示了部分SpEL用法,请往下看XML 有注释,自行添加spring支持即可运行。

applicationContex.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:util="http://www.springframework.org/schema/util"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	<bean id="person" class="tang.Person">
		<!-- 字符串 -->
		<property name="name" value="#{'唐'}"></property>
		<!-- 运算符 + - * / -->
		<property name="livedays" value="#{20 * 365}"></property>
		<!-- 三目运算 -->
		<property name="age" value="#{20 > 30 ? 20 : 30}"></property>
		<!-- 引用bean-->
		<property name="car" value="#{car}"></property>
		<!-- 引用bean的属性 
		<property name="carName" value="#{car.name}"></property> -->
		<!-- 引用bean的实例方法 -->
		<property name="carName" value="#{car.getCarName()}"></property>
		<!-- 引用bean的静态方法 -->
		<property name="staticFun" value="#{car.staticFun()}"></property>
		<!-- 引用的静态方法 -->
		<property name="PI" value="#{T(java.lang.Math).PI}"></property>
		<!-- 获得系统语言 -->
		<property name="sysLanguage" value="#{systemProperties['user.language']}"></property>
		<!-- 还可以正则表达式,以下验证邮箱格式是否正确,正确返回true,错误返回false-->
		<property name="mailAddress" value="#{'[email protected]' matches '^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$'}"></property>
		
	</bean>
	
	<bean id="car" class="tang.Car">
		<property name="name" value="audi"></property>
		<!-- 给构造方法赋值 -->
		<constructor-arg name="price" value="#{250000}"></constructor-arg>
	</bean>
</beans>
TestClass

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Car car = ac.getBean("car",Car.class);
		System.out.println(car);
		Person person = ac.getBean("person",Person.class);
		System.out.println(person);

输出结果

Car [name=audi,price=250000]
Person [name=唐, age=30, carName=this is getCarName, livedays=7300, car=Car [name=audi,price=250000], staticFun=this is staticFun, PI=3.141592653589793, sysLanguage=zh, mailAddress=true ]

class 

Car

package tang;

public class Car {
	public Car() {
	}

	private String name;
	private Integer price;
	
	public Integer getPrice() {
		return price;
	}

	public void setPrice(Integer price) {
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Car [name=" + name + ",price=" + price + "]";
	}
	public String getCarName(){
		return "this is getCarName";
	}

	public static String staticFun(){
		return "this is staticFun";
	}

	public Car(Integer price) {
		super();
		this.price = price;
	}
}

Person

package tang;
public class Person {
	private String name;
	private Integer age;
	private String carName;
	private Integer livedays;
	private Car car;
	private String staticFun;
	private String PI;
	private String sysLanguage;
	private Boolean mailAddress;
	public Boolean getMailAddress() {
		return mailAddress;
	}
	public void setMailAddress(Boolean mailAddress) {
		this.mailAddress = mailAddress;
	}
	public String getSysLanguage() {
		return sysLanguage;
	}
	public void setSysLanguage(String sysLanguage) {
		this.sysLanguage = sysLanguage;
	}
	public String getPI() {
		return PI;
	}
	public void setPI(String pI) {
		PI = pI;
	}
	public String getStaticFun() {
		return staticFun;
	}
	public void setStaticFun(String staticFun) {
		this.staticFun = staticFun;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", carName=" + carName + ", livedays=" + livedays + ", car="
				+ car + ", staticFun=" + staticFun + ", PI=" + PI + ", sysLanguage=" + sysLanguage + ", mailAddress=" + mailAddress + " ]";
	}
	public Person() {
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getCarName() {
		return carName;
	}
	public void setCarName(String carName) {
		this.carName = carName;
	}
	public Integer getLivedays() {
		return livedays;
	}
	public void setLivedays(Integer livedays) {
		this.livedays = livedays;
	}
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
}




猜你喜欢

转载自blog.csdn.net/tangyaliang11/article/details/79259999
今日推荐