使⽤SpringEL表达式进⾏三⽬运算SpEL⽀持三⽬运算符,以此来实现条件语句。
⼀、编写Bean类
spring framework表达式assignItem类:
package com.yiidian.domain;
/**
*
* @author www.yiidian
*
*/
public class Item {
private int qtyOnHand;
public int getQtyOnHand() {
return qtyOnHand;
}
public void setQtyOnHand(int qtyOnHand) {
this.qtyOnHand = qtyOnHand;
}
}
Customer类:
package com.yiidian.domain;
import java.io.Serializable;
/**
*
* @author www.yiidian
*
*/
public class Customer implements Serializable{
private boolean warning;
public boolean isWarning() {
return warning;
}
public void setWarning(boolean warning) {
this.warning = warning;
}
@Override
public String toString() {
return "Customer [warning=" + warning + "]";
}
}
⼆、配置l
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:p="/schema/p" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="
/schema/beans /schema/beans/spring-beans.xsd">
<bean id="customer" class="com.yiidian.domain.Customer">
<property name="warning" value="#{itemBean.qtyOnHand < 100 ? true : false}" />
</bean>
<bean id="itemBean" class="com.yiidian.domain.Item">
<property name="qtyOnHand" value="99" />
</bean>
</beans>
三、编写测试类
package st;
import org.junit.Test;
import t.ApplicationContext;
import t.support.ClassPathXmlApplicationContext;
import com.yiidian.domain.Customer;
/**
* @author www.yiidian
*
*/
public class Demo1 {
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("l");
Customer customer = (Bean("customer");
System.out.println(customer);
}
}
四、运⾏结果
欢迎关注我的::⼀点教程。获得独家整理的学习资源和⽇常⼲货推送。
如果您对我的系列教程感兴趣,也可以关注我的⽹站:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论