今天我想使用单元测试类将一些数据存储在mysql中,但是报告了错误。 org.spring framework.beans.factory.nosuchbeandefinitionexception : nobeannamed ' loca an
检查了很久,实际上没有发现问题,试着手动向javabean注入结构,奇迹般地解决了这个问题。
单元测试的具体写法:
1 .使用注释@Before读取dao层的xml配置文件。 请注意它必须是dao层。 否则,找不到mybatis的会话工厂
2 .为了允许程序获取mapper接口,必须使用注入到服务层的接口实现类。 否则,mapper接口实例的参数将为空,并报告空指针异常
3 .结构注入的service层实现类必须写在dao层的xml文件中,不能在单独注入其他xml后再检索Javabean。 否则,将报告错误地创建了bean。
当然,如果您真的想写在新的xml文件中,可以将新的xml导入到dao层xml配置文件中。
dao层xml文件模板:
1? XML版本=' 1.0 '编码=' utf-8 '?
2
3 xmlns : xsi=' http://www.w3.org/2001/XML架构-instance '
4 xmlns : context=' http://www.spring framework.org/schema/context '
5 xsi :方案位置=' http://www.spring framework.org/schema/beans 63358 www.spring framework.org/schema/beans beand spring-beans.xsd 7http://www.spring framework.org/schema/context 8http://www.spring framework.org/schema/contont
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
视图代码
注册自定义bean的xml文件模板:
1? XML版本=' 1.0 '编码=' utf-8 '?
2
3 xmlns : xsi=' http://www.w3.org/2001/XML架构-instance '
4 xsi :方案位置=' http://www.spring framework.org/schema/beans 5http://www.spring framework.org/schema/beann
6
7
8
9
视图代码