【发布时间】:2014-12-19 10:55:24
【问题描述】:
在一次工作回顾期间,我被分配了一项任务,以实现一个玩具 Singelton 数据库和一个用户控制器,该控制器通过使用 Spring 依赖项注入来使用该数据库。 我推出了这个版本:
辛格尔顿数据库
package SingeltonDBVersion2;
import GlobalSetting.User;
public class SingeltonDB {
private static DBconnImpl db = null;
private static SingeltonDB singalDb = null;
private SingeltonDB(String username, String password) {
db = new DBconnImpl();
}
public synchronized static SingeltonDB getInstance(String username,
String password) throws Exception {
if (db != null) {
return singalDb;
}
System.out.println("The database is now open");
singalDb = new SingeltonDB(username, password);
db.connect(username, password);
System.out.println("The database was connected");
singalDb.create("table1");
return singalDb;
}
public void create(String tableName) throws Exception {
db.create(tableName);
}
public User query(String tableName, int rowID) throws Exception {
if (db == null) {
System.out.println("Error: the database is not open");
return null;
}
return (db.query(tableName, rowID));
}
public void update(String tableName, User user) throws Exception {
if (db == null) {
System.out.println("Error: the database is not open");
return;
}
db.update(tableName, user);
}
}
用户控制器
package SingeltonDBVersion2;
import GlobalSetting.IUserContorller;
import GlobalSetting.User;
/****************************************************************************
* This is class is an implementation of a UserContorller for the version with
* Spring, because here we use inversion of control. The UserContorller
* constructor takes the an object of SingeltonDB which we are passing using
* Spring Dependency Injection in our UnitTests
*****************************************************************************/
public class UserContorller implements IUserContorller {
SingeltonDB db;
public UserContorller(SingeltonDB db) throws Exception {
this.db = db;
}
@Override
public void createTable(String table) throws Exception {
db.create(table);
}
@Override
public void saveUser(String table, int id, String name, int age)
throws Exception {
db.update(table, new User(id, name, age));
}
@Override
public User getUser(String table, int id) throws Exception {
return db.query(table, id);
}
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="MyUserContorller" class="SingeltonDBVersion2.UserContorller">
<constructor-arg ref="MySingeltonDB" />
</bean>
<bean id="MySingeltonDB" class="SingeltonDBVersion2.SingeltonDB"
factory-method="getInstance">
<constructor-arg value="MyAccount" />
<constructor-arg value="123" />
</bean>
</beans>
我可以这样使用:
ApplicationContext contex = new ClassPathXmlApplicationContext("spring.xml");
UserContorller user1 = (UserContorller) contex.getBean("MyUserContorller");
saveUser("table1", 1, Mike, 44); //add user with id 1 and age of 44
现在,我被要求编写一个测试(比如 Junit),如果 SingeltonDB 的 getinstance() 方法不同步,该测试将失败... 我真的不知道该怎么做......
有人可以帮忙提供灵魂吗? 谢谢!
【问题讨论】:
标签: java spring design-patterns asynchronous junit