public class BeanFactory{
private static Propertie props;
static {
try{
props = new Properties();
InputStream in = BeanFactory.class.getClassLoader()
.getResourceAsStream("bean.properties");
props.load(in);
}catch(Exception e){
throw new ExceptionInInitializerError("初始化失败!");
}
}
public Object getBean(String beanName){
Object bean;
try{
String beanPath = props.getProperty(beanName);
bean = Class.forName(beanPath).newInstance();
}catch(Exception e){
e.printStackTrace();
}
return bean;
}
}
public class BeanFactory{
private static Propertie props;
private static Map<String,Object> beans;
static {
try{
props = new Properties();
InputStream in = BeanFactory.class.getClassLoader()
.getResourceAsStream("bean.properties");
props.load(in);
beans = new HashMap<String,Object>();
Enumeration keys = props.keys();
while(keys.hasMoreElements()){
String key = keys.nextElements.toString();
String beanPath = props.getProperty(key);
Object value = Class.forName(beanPath).newInstance();
beans.put(key,value);
}
}catch(Exception e){
throw new ExceptionInInitializerError("初始化失败!");
}
}
public static Object(String beanName){
return beans.get(beanName);
}
}