建Spring的项目实例化bean出错

本文通过一个简单的Spring示例,详细解析了由于属性名不匹配导致的错误,并提供了正确的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 初学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值从一而终,不容易出错这是最好的拉.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值