java unit testing and implementation of the interface call mybatis

Today, I want to use your unit test classes to store some data to mysql, however, has been an error, org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'locationService' available

Check for a long time, refused to concede defeat can not find the problem, tried it manually constructed injection javabean, the miracle solution to this problem.

 

 

Specific writing unit tests:

1. Notes @Before read dao layer of xml configuration files ,, Note that you must be dao layer, or can not find mybatis session factory

 

 

 

2. After the required service class interface configured injection layer, the program can obtain an interface mapper mapper interface instance ,, otherwise the null, the null pointer exception will be reported

 

 

 

 

3. The construction of the injection layer service implementation class <bean> must be written in the xml file dao layer, the other can not be injected in a separate xml and then get Javabean, otherwise it will report an error created bean.

 

Of course, if you really want to write in a new xml file can be imported into a new xml dao layer xml configuration file.

 

 

 

 

dao layer xml file template:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6     http://www.springframework.org/schema/beans/spring-beans.xsd
 7     http://www.springframework.org/schema/context
 8     http://www.springframework.org/schema/context/spring-context.xsd">
 9     <!-- 1.配置数据库相关参数properties的属性:${url} -->
10     <context:property-placeholder location="classpath:jdbc.properties"/>
11 
12     <!-- 配置 数据源 -->
13     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
14         <!-- 驱动 -->
15         <property name="driverClassName" value="${jdbc.driverClassName}"/>
16         <!-- url -->
17         <property name="url" value="${jdbc.url}"/>
18         <!-- 用户名 -->
19         <property name="username" value="${jdbc.username}"/>
20         <!-- 密码 -->
21         <property name="password" value="${jdbc.password}"/>
22     </bean>
23 
24     <!-- 配置 Mybatis的工厂 -->
25     <bean class="org.mybatis.spring.SqlSessionFactoryBean">
26         <!-- 数据源 -->
27         <property name="dataSource" ref="dataSource"/>
28         <!-- 配置Mybatis的核心 配置文件所在位置 -->
29         <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
30         <!-- 配置pojo别名 -->
31         <property name="typeAliasesPackage" value="cn.cen2guo.clinic.entity"/>
32         <!--当mapper中的接口文件与xml文件在同一个包下但是不在同一级时-->
33         <!--需要指定mapper 的xml文件路径,如果在同一级则可不写-->
34         <!-- 否则会报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)-->
35         <property name="mapperLocations" value="classpath:cn/cen2guo/clinic/mapper/mapperXML/*.xml"/>
36     </bean>
37 
38     <!--扫描mapper接口, 写在此包下即可被扫描到 -->
39     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
40         <property name="basePackage" value="cn.cen2guo.clinic.mapper"/>
41     </bean>
42 
43 
44     <!--    导入my_javabean.xml,用于自定义注册构造注入的bean-->
45     <import resource="classpath:myxml/my_javabean.xml"/>
46 </beans>
View Code

 

自定义注册bean的xml文件模板:

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4        xsi:schemaLocation="http://www.springframework.org/schema/beans
5     http://www.springframework.org/schema/beans/spring-beans.xsd">
6     <!-- 位置信息服务接口-->
7     <bean id="locationService" class="cn.cen2guo.clinic.service.serviceImpl.LocationServiceImpl"/>
8 
9 </beans>
View Code

 

Guess you like

Origin www.cnblogs.com/c2g5201314/p/12238976.html