Java如何在Spring EL中使用逻辑表达式?
在上一个示例中,您学习了如何在Spring EL中比较值?在这里,您了解了如何比较两个简单值的相等性(小于或大于)等。在实践中,您可能需要将一个或多个此比较组合为更复杂的逻辑表达式。
Spring EL还提供了几个可用于执行这种逻辑运算的运算符。下表中显示了这些运算符。
运算符 | 描述 |
---|---|
and | 逻辑AND运算;结果将是正确的,双方都是正确的。 |
or | 逻辑或运算;如果至少在侧面为真,则结果为真。 |
not 要么 ! | 逻辑非操作;它将否定表达式值。 |
这是and在spring配置文件中使用逻辑运算符的示例:
<property name="largeGlass" value="#{smallGlass.maxVolume ge 20 and smallGlass.maxVolume le 30}"/>
如果玻璃大于或等于,则上面的配置会将上述largeGlass属性设置为。如果体积介于和之间,则它将仅被视为大玻璃杯。因此,我们在该表达式中使用逻辑和运算符来评估largeGlass属性的值。现在让我们看完整的例子。truemaxVolume20302030
首先让我们定义MyOtherGlasspojo。
package org.nhooo.example.spring.el;public class MyOtherGlass {
private boolean empty;
private boolean halfEmpty;
private int volume;
private int maxVolume;
private boolean largeGlass;
public MyOtherGlass() {
}
public MyOtherGlass(int volume, int maxVolume) {
this.volume = volume;
this.maxVolume = maxVolume;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
public boolean isHalfEmpty() {
return halfEmpty;
}
public void setHalfEmpty(boolean halfEmpty) {
this.halfEmpty = halfEmpty;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public int getMaxVolume() {
return maxVolume;
}
public void setMaxVolume(int maxVolume) {
this.maxVolume = maxVolume;
}
public boolean isLargeGlass() {
return largeGlass;
}
public void setLargeGlass(boolean largeGlass) {
this.largeGlass = largeGlass;
}
}
创建pojo之后,现在让我们创建spring配置文件,在其中定义smallGlassbean和largeGlassbean。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="smallGlass">
<constructor-arg name="volume" value="5"/>
<constructor-arg name="maxVolume" value="10"/>
<property name="largeGlass"
value="#{smallGlass.maxVolume ge 20 and smallGlass.maxVolume le 30}"/>
</bean>
<bean id="largeGlass">
<constructor-arg name="volume" value="5"/>
<constructor-arg name="maxVolume" value="30"/>
<property name="largeGlass"
value="#{largeGlass.maxVolume ge 20 and largeGlass.maxVolume le 30}"/>
</bean>
</beans>
最后,让我们创建一个类以在控制台中运行此配置文件:
package org.nhooo.example.spring.el;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpELLogicalExpression {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spel-logical-expression.xml");
MyOtherGlass smallGlass =
(MyOtherGlass) context.getBean("smallGlass");
MyOtherGlass largeGlass =
(MyOtherGlass) context.getBean("largeGlass");
System.out.println("smallGlass.isLargeGlass() = " + smallGlass.isLargeGlass());
System.out.println("largeGlass.isLargeGlass() = " + largeGlass.isLargeGlass());
}
}
该代码在执行时将为您提供以下输出:
smallGlass.isLargeGlass() = falselargeGlass.isLargeGlass() = true
以上是 Java如何在Spring EL中使用逻辑表达式? 的全部内容, 来源链接: utcz.com/z/326311.html