建Spring的專案例項化bean出錯
阿新 • • 發佈:2019-02-06
HelloBean.java
publicclass HelloBean
{
private String helloword;
publicvoid 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;
publicclass SpringDemo
{
publicstaticvoid 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-311:36:49 org.springframework.core.CollectionFactory <clinit>
資訊: JDK 1.4+ collections available
2007-8-311: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)
publicclass HelloBean
{
private String helloword;
publicvoid setHelloword(String helloWord)
{
this.helloword =helloWord;
}
public String getHelloword()
{
return helloword;
}
}
Beans-config.xml
<!DOCTYPE beans PUBLIC "-//Spring/dtd bean/en
<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.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
publicclass SpringDemo
{
publicstaticvoid main(String args[])
{
Resource rs=new FileSystemResource("Beans-config.xml");
BeanFactory factory
HelloBean hello=(HelloBean)factory.getBean("helloBean123");
System.out.println(hello.getHelloword());
}
}
錯誤提示:
2007-8-311:36:49 org.springframework.core.CollectionFactory <clinit>
資訊: JDK 1.4+ collections available
2007-8-311: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)