使用Properties文件配置实例的创建

来源:互联网 发布:linux如何打开根目录 编辑:程序博客网 时间:2024/05/22 13:15

 目的在一个Properties文件中配置class的类路径,java程序中不需要直接创建对象,而是根据properties配制的类路径进行实例化,优点在于可以在外界改变类的动态类型,应用场景是,面向接口编程中,接口的动态实现类转移到外部配置

 

Properties props=new Properties()
props.load(new FileInputStream("config.properties"));
String budinessClass=props.getProperty("business.class");
Business business=(Business)Class.forName(businessClass).newInstance();