结合Spring表达式语言中的逻辑运算的问题

时间:2013-08-01 07:34:33

标签: spring spring-el

我刚开始使用弹簧版本3.2.2。我试图使用spring表达式语言(SPEL)创建一些示例。但我正面临着问题 在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):意外的令牌。预期'rparen())'但是'literal_int'

我们不能使用弹簧配置文件中提到的括号组合多个逻辑表达式吗?如果没有,那么任何身体都可以建议我正确的方式吗?

1 个答案:

答案 0 :(得分:1)

尝试在gt和le键之前删除 - (减号)(使用&gt;和&lt; =)