2014-01-30 49 views
0

你能告訴我爲什麼我得到這個異常嗎? 我沒有得到通過Spring表達式語言評估的值。春季表達式語言不起作用

我的豆腐看起來是這樣的:

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

我使用spring 3.5版本。你能告訴我是否需要包括任何罐子? – vibhas

回答

0

確保你至少使用Spring 3.0 - >在這個版本中引入了Spring Expression Language上。

請參閱此鏈接瞭解詳細信息:http://docs.spring.io/spring/docs/3.0.x/reference/new-in-3.html

在題目:我猜你#{8}只是一個測試,規劃環境地政司在工作?

+0

是的,只是爲了測試spEL。我正在使用spring 3.5。 – vibhas