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
你在什麼版本的春天? –
我使用spring 3.5版本。你能告訴我是否需要包括任何罐子? – vibhas