初学spring尝试第一个例子,出现很多错误整整调拉两天还要多,当然网上还有很多朋友写啦这类错误的原因,虽相似但不尽相同,所以写下算是对那些给与我提示的朋友感谢,也给后来者一个参考!该例子有三个文件:
HelloBean.java
public class HelloBean

...{
private String helloword;
public void setHelloword(String helloWord)

...{
this.helloword =helloWord;
}
public String getHelloword()

...{
return helloword;
}
}

Beans-config.xml
<!DOCTYPE beans PUBLIC "-//Spring/dtd bean/en" "spring-beans.dtd">
<beans>
<bean id="helloBean123" class="HelloBean">
<property name="helloword">
<value>hello!just in!</value>
</property>
</bean>
</beans>

SpringDemo.java
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
public class SpringDemo

...{
public static void main(String args[])

...{
Resource rs=new FileSystemResource("Beans-config.xml");
BeanFactory factory=new XmlBeanFactory(rs);
HelloBean hello=(HelloBean)factory.getBean("helloBean123");
System.out.println(hello.getHelloword());
}
}

错误提示:
2007-8-3 11:36:49 org.springframework.core.CollectionFactory <clinit>
信息: JDK 1.4+ collections available
2007-8-3 11:36:49 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [D:AllProjectApplictionProjectjavaworkspaceSpringHelloWorldBeans-config.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloBean123' defined in file
[D:AllProjectApplictionProjectjavaworkspaceSpringHelloWorldBeans-config.xml]:
Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'helloword'
of bean class [HelloBean]: Bean property
'helloword' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
org.springframework.beans.NotWritablePropertyException: Invalid property 'helloword' of bean class [HelloBean]: Bean property 'helloword'
is not writable or has an invalid setter method:
Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:668)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:570)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:735)
at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:762)
at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:751)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1069)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:863)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:382)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:144)
这里错误原因就是HelloBean.java中属性的设置的名称“setHelloword()”和Beans-config.xml中<property name="helloword">的不同,在这里我们假设Beans-config.xml中的属性名称不变为helloword,那么你将HelloBean.java中属性设置的方法名称“setHelloword()”改为"setHelloWord()"就不可以,会出现上面所提示错误,当然如果我们直接将我们定义的变量“helloword”之前加“set”和“get”,然后在配置文件中还用它作为property的name值从一而终,不容易出错这是最好的拉.






































































