我是 spring 新手,使用的是 spring 版本 3.2.2。我尝试使用 spring 表达式语言 (SPEL) 创建几个示例。但是我在 spring 配置文件中使用逻辑运算符时遇到了问题。
请找到下面的 spring 配置文件片段。
<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-3.0.xsd">
<bean id="bean3" class="com.springaction.testcollection.PersonBean">
<property name="name" value="Prakash" />
<property name="age" value="62" />
<property name="salary" value="30000" />
<property name="bonus" value="#{30000*T(com.springaction.testcollection.PersonBean).count}" />
</bean>
<bean id="bean4" class="com.springaction.testcollection.PersonBean">
<property name="name" value="Kamini" />
<property name="age" value="60" />
<property name="manager" value="#{bean3.name=='Jinesh' or (bean3.salary -gt 3000 and bean3.age -le 62)}" />
</bean>
</beans>
请找到下面的 PersonBean.java 文件供您参考。
/**
*
*/
package com.springaction.testcollection;
import org.springframework.beans.factory.InitializingBean;
/**
* @author jinesh
*
*/
public class PersonBean implements InitializingBean{
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
String name;
int age;
Float salary;
public static int count=1;
Float bonus;
boolean manager;
PersonBean(){
// System.out.println("******** Constructor gets called ************");
count++;
}
/**
* @return the manager
*/
public boolean isManager() {
return manager;
}
/**
* @param manager the manager to set
*/
public void setManager(boolean manager) {
this.manager = manager;
}
/**
* @return the bonus
*/
public Float getBonus() {
return bonus;
}
/**
* @param bonus the bonus to set
*/
public void setBonus(Float bonus) {
this.bonus = bonus;
}
/**
* @return the count
*/
public static int getCount() {
return count;
}
/**
* @param count the count to set
*/
public static void setCount(int count) {
PersonBean.count = count;
}
/**
* @return the salary
*/
public Float getSalary() {
return salary;
}
/**
* @param salary the salary to set
*/
public void setSalary(Float salary) {
this.salary = salary;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
//System.out.println("***** Setter method of the name gets called **********");
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
//System.out.println("***** Setter method of the age gets called **********");
this.age = age;
}
}
当我尝试使用以下代码获取 bean4 时,我遇到了异常。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.springaction.testcollection.PersonBean;
import com.springaction.testmap.PersonBeanMap;
/**
* @author jinesh
*
*/
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("talentacquisition.xml");
PersonBean pbb3=(PersonBean)context.getBean("bean4");
System.out.println(pbb3.isManager());
}
}
以下是我在执行上述主应用程序代码时遇到的异常。
引起:org.springframework.expression.spel.SpelParseException: EL1043E:(pos 42): Unexpected token。预期为 'rparen())' 但为 'literal_int'
我们不能像spring配置文件中提到的那样使用括号组合多个逻辑表达式吗?如果没有,那么任何机构都可以建议我正确的方法吗?