[applicationContext.xml]: Initialization of bean failed;

错误代码如下


Error creating bean with name 'car' defined in class path resource [applicationContext.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 'double' for property 'price';

nested exception is java.lang.NumberFormatException: For input string: "Spring"


错误原因如下:

我是Spring的初学者

Car类里面我定义了三个属性,一个是String 一个是double 一个是int类型的

我一直以为value=100就是int、integer类型;其实不是;

无论是什么类型的都是value="值"

<bean id="car" class="cn.com.day01.Car" >
<property name="name" value="Spring"></property>
<property name="price" value=90 ></property>
<property name="speed" value=100 ></property>
</bean>

由于上述的配置会导致整个xml文件报错,我就把value全部去掉了,然后在测试类里面这样写

 //1.创建IOC容器
    ApplicationContext ioc=new  ClassPathXmlApplicationContext("applicationContext.xml");
    //2.获取bean
    Car c=(Car) ioc.getBean("car");
    c.setName("宝马");
    c.setPrice(1200);
    c.setSpeed(90);
    System.out.println(c.toString());

在bean配置文件里面,就已经给属性初始化赋值了,

正确的应该是这样的

<bean id="car" class="cn.com.day01.Car" >
<property name="name" value="Spring"></property>
<property name="price" value="90" ></property>
<property name="speed" value="100" ></property>
</bean>
//1.创建IOC容器
	ApplicationContext ioc=new  ClassPathXmlApplicationContext("applicationContext.xml");
	//2.获取bean
	Car c=(Car) ioc.getBean("car");

	System.out.println(c.toString());

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_37591637

请给我持续更新的动力~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值