JAXP(JAVA API XML PRECESSING) ,即是通过配置文件来处理java的api
package jaxp;
/**
* @author huzhengxing
* @description:一些javaBean
* @date 2017/9/21 9:34
*/
public class Bean {
}
interface A {
void show();
}
class AImpl implements A {
@Override
public void show() {
System.out.println("show A bean");
}
}
interface B {
void show();
}
class BImpl implements B {
@Override
public void show() {
System.out.println("show B bean");
}
}
package jaxp; import java.io.FileReader; import java.io.IOException; import java.util.Properties; /** * @author huzhengxing * @description: 获取bean的工厂 * @date 2017/9/21 9:30 */ public class BeanFactory { private static Properties prop = new Properties(); static { //读取配置文件 try { prop.load(new FileReader("src/jaxp/beanFactory.properties")); } catch (IOException e) { e.printStackTrace(); } } public static <T> T getBean(Class<T> c) { try { //获取实现类 String className = prop.getProperty(c.getSimpleName()); //返回类的对象 Class<T> tClass = (Class<T>) Class.forName(className); return tClass.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { BeanFactory.getBean(B.class).show(); } }
#配置文件
A=jaxp.AImpl B=jaxp.BImpl