📄 beanfactory.java
字号:
package com.briup.util;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Properties;
import com.briup.dao.IDao;
import com.briup.service.IService;
import com.briup.transaction.Transaction;
/**
*
* 通过工厂方式获取实例对象
* @author Administrator
*
*/
public class BeanFactory {
private static IDao dao;
private static IService service;
private static Transaction tran;
/*
* @return dao的实例
*/
public static IDao getDao() throws Exception {
Properties props = getProperties();
String className = props.getProperty("DaoImpl");
if (dao == null)
dao = (IDao) Class.forName(className).newInstance();
return dao;
}
/*
* @return service的实例
*/
/**
* @return Iservire
* @throws Exception
*/
public static IService getService() throws Exception {
Properties props = getProperties();
String classNameImpl = props.getProperty("ServiceImpl");
String classNameProxy = props.getProperty("ServiceProxy");
IService iService = null;
if (service == null) {
iService = (IService) Class.forName(classNameImpl).newInstance();
Constructor constructor = Class.forName(classNameProxy)
.getConstructor(IService.class);
service = (IService) constructor
.newInstance(new Object[] { iService });
}
return service;
}
/**
* @return Transaction
* @throws Exception
*/
/*
* @return 事务类实例
*/
public static Transaction getTransaction()throws Exception{
Properties props = getProperties();
String className = props.getProperty("TransactionImpl");
tran = (Transaction) Class.forName(className).newInstance();
return tran;
}
/*
* 从woss.txt文件中加载以等号分割的信息为键值对
* @return Properties封装的键值对
*/
private static Properties getProperties() {
Properties props = new Properties();
try {
props.load(BeanFactory.class.getResourceAsStream("../woss.txt"));
} catch (IOException e) {
e.printStackTrace();
}
return props;
}
public static void main(String[] args) {
try {
System.out.println(getDao());
System.out.println(getService());
System.out.println(getTransaction());
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -