注入失败问题

来源:3-8 持久层实现——StudentDAO和CourseDAO

weixin_慕勒8187086

2020-09-19 22:49:03

在studentDaoImp类里面的 RowMapper 对Student类用了个属性注入, 运行时报空指针异常,为什么没有注入成功? 不用属性注入,直接new 一个实例就可以测试成功。

http://img.mukewang.com/climg/5f6619bf0841747218010774.jpgspring config

http://img.mukewang.com/climg/5f6619e308d6c45314470494.jpgStudentDaoImp 属性注入

http://img.mukewang.com/climg/5f661a3608211c3916450607.jpg

写回答

2回答

好帮手慕阿慧

2020-09-20

同学你好,私有类StudentRowMapper没有被Srping容器管理,所以不会注入Student对象,Student对象为null。注入Student对象是将Spring容器中实例化的Student对象注入。

在StudentDaoImpl类中注入Stduent对象就可以,是因为StudentDaoImpl类被Spring容器管理了。

祝学习愉快~

0

好帮手慕阿慧

2020-09-20

同学你好,报空指针异常是因为注入的Student对象失败。私有类StudentRowMapper中student为null。如下:

http://img.mukewang.com/climg/5f66c11709fb5b0408470331.jpg

解决方法:建议同学在StudentDaoImpl类中注入Stduent对象。

参考代码如下:

http://img.mukewang.com/climg/5f66c171096b0b2507470554.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快~

0
heixin_慕勒8187086
h 1. 为什么私有类StudentRowMapper中student为null?注入Student对象不是相当于new 了一个Student的instance吗?mapRow方法不是给student这个实例set了值吗? 2. 为什么在StudentDaoImpl类中注入Stduent对象就可以?
h020-09-20
共1条回复

0 学习 · 8263 问题

查看课程

相似问题