Spring Expression Language无效

时间:2014-01-30 16:15:15

标签: spring

你能告诉我为什么我得到这个例外吗? 我没有通过Spring Expression Language获得评估值。

我的bean看起来像这样:

package org.vibhas.spring;

public class Triangle {

    int length;
    public int getLength() {
        return length;
    }
    public void setLength(int length) {
        this.length = length;
    }
    public int getBreadth() {
        return breadth;
    }
    public void setBreadth(int breadth) {
        this.breadth = breadth;
    }
    int breadth;
    int area;
    public int getArea() {
        return area;
    }
    public void setArea(int area) {
        this.area = area;
    }
    public void shape(){
        System.out.println("Printing Triangle area:"+length*breadth);
        System.out.println("Area:"+area);
    }

    }

这是我的xml配置:

<?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="triangle"
          class="org.vibhas.spring.Triangle" >
          <property name="length" value="4"/>
          <property name="breadth" value="2"/>
          <property name="area" value="#{8}"/>

    </bean>
</beans>

在调用shape()方法时,我遇到以下异常:

org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions INFO: Loading XML bean definitions from file [C:\Users\Sony\workspace\SpringTutorial\spring.xml] 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in file [C:\Users\Sony\workspace\SpringTutorial\spring.xml]: Initialization of bean failed; 
nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'area'; 
nested exception is java.lang.NumberFormatException: For input string: "{8}"    
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFacto.java:299)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.vibhas.spring.Drawing.main(Drawing.java:15) Caused by:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'area'; 
nested exception is java.lang.NumberFormatException: For input string: "{8}"    
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:479)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:505)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:499)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1456)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1415)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1151)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    ... 6 more Caused by: java.lang.NumberFormatException: For input string: "{8}"  
    at java.lang.NumberFormatException.forInputString(Unknown Source)   
    at java.lang.Integer.parseInt(Unknown Source)   
    at java.lang.Integer.valueOf(Unknown Source)    
    at java.lang.Integer.decode(Unknown Source)     
    at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:155)
    at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:115)
    at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:427)
    at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:400)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:181)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:459)
    ... 12 more

1 个答案:

答案 0 :(得分:0)

确保您至少使用Spring 3.0 - &gt; Spring Expression Language在此版本中引入。

有关详细信息,请参阅此链接:http://docs.spring.io/spring/docs/3.0.x/reference/new-in-3.html

关于主题:我猜您的#{8}只是SpEL正在运行的测试?