最近我们部门有个小项目,用来管理这个公司所有项目用到的代码表,例如国家代码、行政区划代码等。这个项目的功能其实很少,就是简单的修改、查询、新增和逻辑删除。但是为每张表都写一套增删改查的页面和一套service,工作量巨大,且维护很困难。我们发现各个表的业务其实都很类似,如果能写一套通用的service代码,在web层根据表名动态调用,在通用的jsp上生成想要的数据,那就只需要写一套代码就可以完成所有代码表的操作了。

  完成这个要求第一个想到的当然是用反射啊,通过表名反射生成相应的QO、DAO,通过调用相应的dao方法,来实现相关功能。但是当我通过反射实例出dao,再执行相应的方法时,方法内一直报错。通过查询资料,可以通过方法得到已经被Spring托管的bean( XX.getBean("xxx") ),于是我改成根据表名动态获取dao,通过反射找到相应方法,执行相应方法,问题就迎刃而解了。

  

  首先是获取被Spring托管类的工具类

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service; /**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
*/
@Service
public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class); /**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
logger.debug("注入ApplicationContext到SpringContextHolder:" + applicationContext); if (SpringContextHolder.applicationContext != null) {
logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:"
+ SpringContextHolder.applicationContext);
} SpringContextHolder.applicationContext = applicationContext; //NOSONAR
} /**
* 实现DisposableBean接口,在Context关闭时清理静态变量.
*/
@Override
public void destroy() throws Exception {
SpringContextHolder.clear();
} /**
* 取得存储在静态变量中的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 clear() {
logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
applicationContext = null;
} /**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
}

之后是service层代码

 public interface CommonService {

     /**
* 分页查询
* @param object 表名
* @return Page<> 列表
*/
Object findPageList(String name);
}
 /**
*
* 通用service
*/
@Service("commonService")
public class CommonServiceImpl implements CommonService {
/**
* 分页查询
* @param object 表名
* @return Page<> 列表
*/
public Object findPageList(String name) {
try {
//获取QO
Class clQO = Class.forName("org.xxx.domain.qo."+name+"QO");
Object objectQO = clQO.newInstance();
//设置未删除
Field deltIndc = clQO.getDeclaredField("deltIndc");
deltIndc.setAccessible(true);
deltIndc.set(objectQO, "0");
//首字母小写 拼成xxxxDao
String nameDao =
(new StringBuilder()).append(
Character.toLowerCase(name.charAt(0))).append(name.substring(1)).toString()
+"Dao";
//获取被Spring托管的dao层实例
Object objDao = SpringContextHolder.getBean(nameDao);
Method[] ms = objDao.getClass().getMethods();
for(Method m:ms){
//找到find方法 执行
if("find".equals(m.getName())){
return m.invoke(objDao, new Object[]{objectQO});
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

controller

 @Controller
public class CommonAction { @Autowired(required = false)
private CommonService commonService; @RequestMapping("getPage")
public @ResponseBody Object getPage(String name) {
//"PltmPubNews"
return commonService.findPageList(name);
} }

  这样就可以根据表名动态查询了。

相关链接  :   http://blog.csdn.net/gaopeng0071/article/details/50511341

最新文章

  1. STM32f103 定时器之编码器接口模式
  2. Python自动化之django的ORM操作——Python源码
  3. JavaScript 日历
  4. xsocks 64位平台下编译问题小记
  5. [转]IOS上路_01-Win7+VMWare9+MacOSX10.8+XCode4.6.3
  6. 第一个Sprint冲刺第一天
  7. Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南)
  8. [BZOJ 1115] [POI2009] 石子游戏Kam 【阶梯博弈】
  9. HDU -- 4496
  10. 变形课(dfs)
  11. 创建Windows类别
  12. Fedora20-Xfce音频视频解码器及解决无声音问题[已解决]
  13. 快学Scala-第八章 继承
  14. cocoa pods 命令不执行command not found
  15. HTTP请求过程(http是一种无状态协议,即不建立持久的连接)
  16. sonar-代码扫描
  17. 盯着这where or 终于出了点感觉
  18. python3 第三十二章 - 标准库概览
  19. css效果文字多了就...
  20. 微信小程序开发总结(一)

热门文章

  1. Oracle修改指定表空间为自动扩展
  2. How To Scan QRCode For UWP (1)
  3. linux zip命令
  4. C#基础篇二变量与变量类型
  5. js便签笔记(8)——js加载XML字符串或文件
  6. mybatis JdbcTypeInterceptor - 运行时自动添加 jdbcType 属性
  7. EOS 理解
  8. springboot 入门
  9. SpringCloud入门之eclipse新建maven子项目和聚合项目
  10. springboot redis 缓存跨域丢失问题