Question

I am new to spring and using the spring version 3.2.2. I have tried to create few examples using the spring expression language(SPEL). But i am facing issue while using logical operator in the spring configuration file.

Please find the below snippet of the spring configuration file.

<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>

Please find the below PersonBean.java file for your reference.

/**
 * 
 */
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;
    }


}

When I am trying to obtain the bean4 using the below code I am getting exception.

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());
       }
    }

Below is the exception I am getting while executing the above main application code.

Caused by: org.springframework.expression.spel.SpelParseException: EL1043E:(pos 42): Unexpected token. Expected 'rparen())' but was 'literal_int'

Can't we combine more than one logical expression using parentheses as mentioned in the spring configuration file ? if no then can any body suggest me the correct way?

Was it helpful?

Solution

try remove - (minus) in front of gt and le keywork (ou use > and <=)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top