加载指定目录的所有class,通过注释区分实体类
package com.annotation.annoBean;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.annotation.HBID;
import com.annotation.HBean;
import com.tools.ClassLoaderUtil;
public class AnnotationBeanLoader {
private static String beanRootPath = "/com/annotation";//bean root
public static Map<String,Class<HBean>> loadHandlerBean() throws Exception{
Map<String, Class<HBean>> beanMap = new HashMap<String, Class<HBean>>();
List<Class<?>> classes = ClassLoaderUtil.loadClassesFromPath(beanRootPath);
for (int i = 0; i < classes.size(); i++) {
Class<?> c = classes.get(i);
if(classes.get(i).isAnnotationPresent(HBID.class)){
HBID hbid = classes.get(i).getAnnotation(HBID.class);
String id = hbid.catalog()+"-"+hbid.id();
if(c.isAssignableFrom(HBean.class))
beanMap.put(id, (Class<HBean>) c);
}
}
return beanMap;
}
}
获取子类:
http://blackproof.iteye.com/blog/2015036