0
我是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):意外的標記。 ''''''''''''''''''''''但是'literal_int'
我們不能像Spring配置文件中提到的那樣使用括號來組合多個邏輯表達式嗎?如果沒有,那麼任何一個機構能否告訴我正確的方法?
感謝您的回答bellabax。 – Beast