动态获取实体类及类中方法

动态获取实体类及类中方法, 

1,建立监听器 OpenAPIListener 
public interface OpenAPIListener {
JSONObject handlerData(Map<String, Object> map);
}

2,
  获取实体类名称beanName 并通过getApplicationContext().getBean()方法获取实体类
String beanName = methodInfo[0] + "OpenService";
OpenAPIListener openAPIListener = (OpenAPIListener) SpringContextHolder.getApplicationContext().getBean(beanName);
3,建立实体SpringContextHolder实现ApplicationContextAware和DispossableBean接口
@Component
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

private static ApplicationContext applicationContext = null;

private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);

/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationContext;
}

/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
assertContextInjected();
return (T) applicationContext.getBean(name);
}

/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> requiredType) {
assertContextInjected();
return applicationContext.getBean(requiredType);
}

/**
* 清除SpringContextHolder中的ApplicationContext为Null.
*/
public static void clearHolder() {
if (logger.isDebugEnabled()) {
logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
}
applicationContext = null;
}

/**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext;
}

/**
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
*/
@Override
public void destroy() throws Exception {
SpringContextHolder.clearHolder();
}

/**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
Validate.validState(applicationContext != null,
"applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
}

/**
* 获取接口的实现类实例。
*
* @param clazz
* @return
*/
public static <T> Map<String, T> getImplInstance(Class<T> clazz){
return applicationContext.getBeansOfType(clazz);
}
}
4,建立具体的实体类并重写handlerData方法
@Service("ProjectTrainingOpenService")
public class ProjectTrainingOpenService<ProjectTrainingOpenService> implements OpenAPIListener {
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Transactional(rollbackFor = Exception.class)
public JSONObject handlerData(Map<String, Object> map) {
String method = MapUtils.getString(map, "method");
JSONObject jobj = (JSONObject) MapUtils.getObject(map, "data");
if (method.equals(MethodsConstants.PROJECTTRAINING_ADD)) {
Project project = (Project) MapUtils.getObject(map, "project");
return add(jobj,project);
} else if (method.equals(MethodsConstants.PROJECTTRAINING_QUERY)) {
Project project = (Project) MapUtils.getObject(map, "project");
return query(jobj, project);
} else {

}
return null;
}
}

posted @ 2020-01-03 14:38  fengfeng21  阅读(1300)  评论(0编辑  收藏  举报