public static <T>T newInstance(Class<T> interfaceClass){
try {
//
// UserDao.class
String name = interfaceClass.getSimpleName();
//
String className=findClassNameByName(name);
//
Class<?> aClass = Class.forName(className);
Object o = aClass.newInstance();
return (T) o;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private static String findClassNameByName(String name) throws DocumentException {
//
SAXReader saxReader = new SAXReader();
//
InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml");
//doc
Document document = saxReader.read(in);
//xpath
Element element = (Element) document.selectSingleNode("//bean[@id="" + name + ""]");
//
return element.attributeValue("class");
}