前言:本文需对反射和抽象工厂模式有一定了解。
了解->Java反射
了解->抽象工厂模式
IUser接口
public interface IUser {
void Insert(User user);
User getUser(String id);
}
SqlserverUser
public class SqlserverUser implements IUser {
@Override
public void Insert(User user) {
System.out.println("在SqlserverUser中插入一条数据");
}
@Override
public User getUser(String id) {
System.out.println("在SqlserverUser中查询一条数据");
return null;
}
}
AccessUser
public class AccessUser implements IUser {
@Override
public void Insert(User user) {
System.out.println("在AccessUser中插入一条数据");
}
@Override
public User getUser(String id) {
System.out.println("在AccessUser中查询一条数据");
return null;
}
}
IDepartment
public interface IDepartment {
void Insert(Department department);
Department getDepartment(String id);
}
SqlserverDepartment
public class SqlserverDepartment implements IDepartment{
@Override
public void Insert(Department department) {
System.out.println("在SqlserverDepartment中插入一条数据");
}
@Override
public Department getDepartment(String id) {
System.out.println("在SqlserverDepartment中查询一条数据");
return null;
}
}
AccessDepartment
public class AccessDepartment implements IDepartment {
@Override
public void Insert(Department department) {
System.out.println("在AccessDepartment中插入一条数据");
}
@Override
public Department getDepartment(String id) {
System.out.println("在AccessDepartment中查询一条数据");
return null;
}
}
DataAccess
public class Data {
private static String db;
public static IUser CreateUser() throws IOException, InstantiationException, IllegalAccessException
{
Properties p = new Properties();
try {
InputStream in =Data.class.getClassLoader().getResourceAsStream("database.properties");
p.load(in);}
catch (Exception e) {
e.printStackTrace();
}
db=p.getProperty("DB");
String className = "抽象工厂模式." + db +"User";
//获取以className字符串为名的类
Class<IUser> userDaoImplClass = null;
try {
userDaoImplClass = (Class<IUser>) Class.forName(className);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
//返回类的实例
try {
return userDaoImplClass.getConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static IDepartment CreateDepartment() throws IOException, InstantiationException, IllegalAccessException
{
Properties p = new Properties();
try {
InputStream in =Data.class.getClassLoader().getResourceAsStream("database.properties");
p.load(in);}
catch (Exception e) {
e.printStackTrace();
}
db=p.getProperty("DB");
String className = "抽象工厂模式." + db +"User";
//获取以className字符串为名的类
Class<IDepartment> DepartmentDaoImplClass = null;
try {
DepartmentDaoImplClass = (Class<IDepartment>) Class.forName(className);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
//返回类的实例
try {
return DepartmentDaoImplClass.getConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
配置文件需注意的问题
getClassLoader().getResourceAsStream()的参数是根目录src下的路径,即使配置文件在同目录下,也不能下成getClassLoader().getResourceAsStream(“配置文件”),这样程序会报错,得到的InputStream是null值。
getClassLoader()和getURLClassLoader()的区别:ClassLoader()只能查找src目录下的文件,而URLClassLoader()则能查找任意目录下的文件。