一 , ResourceBundle 用法 与 properties 对比
(1) , ResourceBundle 它只能用于读取 , 不能用于写入
properties 可以读取,可以写入
(2) , ResourceBundle 只能用于读取properties结尾的文件 ResourceBundle.getBundle(“bean”);
properties 可以读取任意结尾的文件, 以流的形式任意读取
in = BeanFactory2.class.getClassLoader().getResourceAsStream(“bean.properties”);
(3), ResourceBundle 只能读取类路径下的properties文件,别的地方不行
com.SH.config.bean(包的方式) 正确 com./H/config/bean (路劲方式)错误的
1, ResourceBundle 用法
public class BeanFactory {
private static ResourceBundle bundle;
static {
// 实列bundle
bundle = ResourceBundle.getBundle(“bean”);
}
public static Object getBean(String beanName){
Object bean = null;
try {
String beanPath = bundle.getString(beanName);
bean = (IUserDao) Class.forName(beanPath).newInstance();
return bean;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
2 , properties 用法
public class BeanFactory2 {
private static Properties props;
static {
InputStream in = null;
try {
in = BeanFactory2.class.getClassLoader().getResourceAsStream("bean.properties");
props = new Properties();
props.load(in);
} catch (IOException e) {
//
}finally {
if (in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static Object getBean(String beanName){
Object bean = null;
try {
String beanPath = props.getProperty(beanName);
bean = (IUserDao) Class.forName(beanPath).newInstance();
return bean;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}