- 在 Maven
项目中用 jUnit
做测试,由于项目结构问题,配置文件不得不置于
CLASSPATH 下;
为了从
CLASSPATH
下读取配置文件、获得输入流以供
MyBatis
使用,编写工具类
tfw.integration_demo.util.ClasspathFileToolT(P/E)
,原理如下:
String strFileName = ……;
URL url = ClasspathFileToolT.class.getClassLoader().getResource(strFileName);
InputStream is = url.openStream();
return is;
- 编写 ApplicationContext
分项配置文件
src/main/resources/tfw/integration_demo/_04_spring_web_mvc/sub_files/04_mybatis_crud.xml
,内容暂时留空:
<?xml version="1.0" encoding="UTF-8"?>
<beans ……>
</beans>
- 修改 ApplicationContext
主配置文件
src/main/resources/tfw/integration_demo/_04_spring_web_mvc/applicationContext.xml
,添加上述分项:
<?xml version="1.0" encoding="UTF-8"?>
<beans ……>
……
<import resource="sub_files/03_jdbc_template_crud.xml"/>
<import resource="sub_files/04_mybatis_crud.xml"/>
</beans>
- 修改 ApplicationContext
分项配置文件
src/main/resources/tfw/integration_demo/_04_spring_web_mvc/sub_files/04_mybatis_crud.xml
,注册所需的各种对象及其注入关系:
<?xml version="1.0" encoding="UTF-8"?>
<beans ……>
<bean id="crud.mybatis.manual_dao.ssfb"
class="org.apache.ibatis.session.SqlSessionFactoryBuilder"/>
<bean id="crud.mybatis.manual_dao.mybatis_cfg_is"
class="tfw.integration_demo.util.ClasspathFileToolP"
factory-method="getURLInputStream" destroy-method="close">
<constructor-arg index="0"
value="tfw/integration_demo/_04_spring_web_mvc/_02_crud/_03_mybatis_crud/mybatis-config.xml"/>
</bean>
<bean id="crud.mybatis.manual_dao.ssf"
factory-bean="crud.mybatis.manual_dao.ssfb" factory-method="build">
<constructor-arg ref="crud.mybatis.manual_dao.mybatis_cfg_is"/>
</bean>
<bean id="crud.mybatis.manual_dao.userDAO"
class="tfw.integration_demo._04_spring_web_mvc._02_crud._03_mybatis_crud._01_manual_dao_way.dao.impl.UserDAO">
<constructor-arg ref="crud.mybatis.manual_dao.ssf"/>
</bean>
<bean id="crud.mybatis.manual_dao.userMgrSvc"
class="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.service._01_UserMgrService">
<property name="userDAO" ref="crud.mybatis.manual_dao.userDAO"/>
</bean>
</beans>
说明 /
注意事项:
- “<bean id="crud.mybatis.manual_dao.mybatis_cfg_is class="……" factory-method="……" ……>……</bean>”:
以“静态工厂”方式创建对象,即调用某个类的某个有返回值的静态方法、接收其返回值,并指定
ID
“crud.mybatis.manual_dao.mybatis_cfg_is”。
- “class="tfw.integration_demo.util.ClasspathFileToolP"”、“factory-method="getURLInputStream"”:
创建对象时所用的“工厂”类和“工厂”方法。
- “destroy-method="close"”:
生成的输入流是
JVM
之外的系统资源,在程序结束前应该关闭、释放;在此调用输入流对象的“close()”方法,关闭、释放资源。
- “<constructor-arg ……/>”:
以“工厂”方式创建对象时,传入“工厂”方法的参数可理解为用来“构造”目标对象,参数的声明办法同“构造方法”。
- “index="0"”:
只有一个参数时,参数无顺序可言,可以不写参数索引号。
- “value="tfw/integration_demo/_04_spring_web_mvc/_02_crud/_03_mybatis_crud/mybatis-config.xml"”:
参数值是以
CLASSPATH
为起点的文件名。
- “<bean id="crud.mybatis.manual_dao.ssf" ……>……</bean>”:
以“实例工厂”方式创建对象,即调用某个实例的某个有返回值的方法、接收其返回值,并指定
ID
“crud.mybatis.manual_dao.ssf”。
- “factory-bean="crud.mybatis.manual_dao.ssfb"”:
创建本对象时所用的“工厂”对象。
- 新建测试用例
tfw.integration_demo._04_spring_web_mvc._02_crud._03_mybatis_crud._01_manual_dao_way.user_mgr_testcase._03_MyBatisDAO_and_Service_with_Spring
:
- 利用工具类
tfw.integration_demo.util.ClasspathFileToolT(P/E)
从
CLASSPATH
下加载
MyBatis
主配置文件;
- 逐步将各对象改为从
ApplicationContext
中获取;
- CRUD 测试沿用
tfw.integration_demo._04_spring_web_mvc._02_crud._03_mybatis_crud._01_manual_dao_way.user_mgr_testcase._02_MyBatisDAO_and_Service
的代码。
……
public class _03_MyBatisDAO_and_Service_with_Spring
{
private String strSpringClassPathConfFile =
"tfw/integration_demo/_04_spring_web_mvc/applicationContext.xml";
private AbstractApplicationContext actxt;
private String strMyBatisClassPathConfFile =
"tfw/integration_demo/_04_spring_web_mvc/_02_crud/_03_mybatis_crud/mybatis-config.xml";
private I_UserDAO userDAO;
private _01_UserMgrService userMgrSvc;
@Test
public void testObjects() throws IOException
{
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
System.out.println("SqlSessionFactoryBuilder:\n\t" + ssfb);
System.out.println("MyBatis Configuration File:\n\t"
+ strMyBatisClassPathConfFile);
InputStream isMyBatisCfg = ClasspathFileToolP
.getURLInputStream(strMyBatisClassPathConfFile);
System.out.println(
"MyBatis Configuration InputStream:\n\t" + isMyBatisCfg);
SqlSessionFactory ssf = ssfb.build(isMyBatisCfg);
System.out.println("SqlSessionFactory:\n\t" + ssf);
……
}
@Test
public void testObjects2() throws IOException
{
System.out.println(
"Spring Configuration File:\n\t" + strSpringClassPathConfFile);
AbstractApplicationContext actxt =
new ClassPathXmlApplicationContext(strSpringClassPathConfFile);
System.out.println("Application Context:\n\t" + actxt);
// SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
SqlSessionFactoryBuilder ssfb = actxt.getBean(
"crud.mybatis.manual_dao.ssfb", SqlSessionFactoryBuilder.class);
System.out.println("SqlSessionFactoryBuilder:\n\t" + ssfb);
// System.out.println("MyBatis Configuration File:\n\t"
// + strMyBatisClassPathConfFile);
// InputStream isMyBatisCfg = ClasspathFileToolP
// .getURLInputStream(strMyBatisClassPathConfFile);
InputStream isMyBatisCfg = actxt.getBean(
"crud.mybatis.manual_dao.mybatis_cfg_is", InputStream.class);
System.out.println(
"MyBatis Configuration InputStream:\n\t" + isMyBatisCfg);
isMyBatisCfg.close();
System.out.println(
"MyBatis Configuration InputStream Closed:\n\t" + isMyBatisCfg);
// SqlSessionFactory ssf = ssfb.build(isMyBatisCfg);
SqlSessionFactory ssf = actxt.getBean("crud.mybatis.manual_dao.ssf",
SqlSessionFactory.class);
System.out.println("SqlSessionFactory:\n\t" + ssf);
SqlSession session = ssf.openSession();
System.out.println("SqlSession:\n\t" + session);
session.close();
System.out.println("SqlSession Closed:\n\t" + session);
// I_UserDAO userDAO = new UserDAO(ssf);
I_UserDAO userDAO = actxt.getBean("crud.mybatis.manual_dao.userDAO",
I_UserDAO.class);
System.out.println("DAO:\n\t" + userDAO);
// _01_UserMgrService userMgrSvc = new _01_UserMgrService();
// userMgrSvc.setUserDAO(userDAO);
_01_UserMgrService userMgrSvc = actxt.getBean(
"crud.mybatis.manual_dao.userMgrSvc", _01_UserMgrService.class);
userMgrSvc.setUserDAO(userDAO);
System.out.println("Service:\n\t" + userMgrSvc);
actxt.close();
System.out.println("Application Context Closed:\n\t" + actxt);
}
@Before
public void init() throws IOException
{
System.out.println(
"Spring Configuration File:\n\t" + strSpringClassPathConfFile);
actxt = new ClassPathXmlApplicationContext(strSpringClassPathConfFile);
System.out.println("Application Context:\n\t" + actxt);
userDAO = actxt.getBean("crud.mybatis.manual_dao.userDAO",
I_UserDAO.class);
System.out.println("DAO:\n\t" + userDAO);
userMgrSvc = actxt.getBean("crud.mybatis.manual_dao.userMgrSvc",
_01_UserMgrService.class);
System.out.println("Service:\n\t" + userMgrSvc);
}
@After
public void close()
{
actxt.close();
System.out.println("Application Context Closed:\n\t" + actxt);
}
@Test
public void testUserDAO_create() throws SQLException
{
……
}
……
}