程序间的依赖关系
开发中遵循的原则:
编译时不依赖,运行时才依赖。
解决依赖关系:
使用反射常见类的 对象。
使用反射创建对象对象引发的新问题:
全类名在程序中写死,需要修改时,需要修改时,需要改源码。
引入配置文件
使用配置文件,通过读取配置文件通过反射创建类对象。
//读取配置文件
private static ResourceBundle bundle=ResourceBundle .getbundle("配置文件名");//类路径下
public static Object getBean(String beanName){
try{
String beanPath=bundle.getString(beanName);
return Class.forName("beanPath").newInstance();
}catch(Exection e){
throw new RuntimeException(e);
}
}