反射和动态加载bean 完成 通用servie

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

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

  

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

 1 import org.slf4j.Logger;
 2 import org.slf4j.LoggerFactory;
 3 import org.springframework.beans.factory.DisposableBean;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.ApplicationContextAware;
 6 import org.springframework.stereotype.Service;
 7 
 8 /**
 9  * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
10  */
11 @Service
12 public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
13 
14     private static ApplicationContext applicationContext = null;
15 
16     private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);
17 
18     /**
19      * 实现ApplicationContextAware接口, 注入Context到静态变量中.
20      */
21     public void setApplicationContext(ApplicationContext applicationContext) {
22         logger.debug("注入ApplicationContext到SpringContextHolder:" + applicationContext);
23 
24         if (SpringContextHolder.applicationContext != null) {
25             logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:"
26                     + SpringContextHolder.applicationContext);
27         }
28 
29         SpringContextHolder.applicationContext = applicationContext; //NOSONAR
30     }
31 
32     /**
33      * 实现DisposableBean接口,在Context关闭时清理静态变量.
34      */
35     @Override
36     public void destroy() throws Exception {
37         SpringContextHolder.clear();
38     }
39 
40     /**
41      * 取得存储在静态变量中的ApplicationContext.
42      */
43     public static ApplicationContext getApplicationContext() {
44         assertContextInjected();
45         return applicationContext;
46     }
47 
48     /**
49      * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
50      */
51     @SuppressWarnings("unchecked")
52     public static <T> T getBean(String name) {
53         assertContextInjected();
54         return (T) applicationContext.getBean(name);
55     }
56 
57     /**
58      * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
59      */
60     public static <T> T getBean(Class<T> requiredType) {
61         assertContextInjected();
62         return applicationContext.getBean(requiredType);
63     }
64 
65     /**
66      * 清除SpringContextHolder中的ApplicationContext为Null.
67      */
68     public static void clear() {
69         logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
70         applicationContext = null;
71     }
72 
73     /**
74      * 检查ApplicationContext不为空.
75      */
76     private static void assertContextInjected() {
77         if (applicationContext == null) {
78             throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
79         }
80     }
81 }

之后是service层代码

1 public interface CommonService {
2     
3     /**
4      * 分页查询
5      * @param object  表名
6      * @return Page<>  列表
7      */
8     Object findPageList(String name);
9 }
 1 /**
 2  * 
 3  * 通用service
 4  */
 5 @Service("commonService")
 6 public class CommonServiceImpl implements CommonService {
 7     /**
 8      * 分页查询
 9      * @param object  表名
10      * @return Page<>  列表
11      */
12     public Object findPageList(String name) {
13         try {
14             //获取QO
15             Class clQO = Class.forName("org.xxx.domain.qo."+name+"QO");
16             Object objectQO = clQO.newInstance();
17             //设置未删除
18             Field deltIndc = clQO.getDeclaredField("deltIndc");
19             deltIndc.setAccessible(true);
20             deltIndc.set(objectQO, "0");
21             //首字母小写    拼成xxxxDao
22             String nameDao = 
23                     (new StringBuilder()).append(
24                             Character.toLowerCase(name.charAt(0))).append(name.substring(1)).toString()
25                             +"Dao";
26             //获取被Spring托管的dao层实例
27             Object objDao = SpringContextHolder.getBean(nameDao);
28             Method[] ms = objDao.getClass().getMethods();
29             for(Method m:ms){
30                 //找到find方法 执行
31                 if("find".equals(m.getName())){
32                     return m.invoke(objDao, new Object[]{objectQO});
33                 }
34             }
35         } catch (Exception e) {
36             e.printStackTrace();
37         }
38         return null;
39     }
40 }

controller

 1 @Controller
 2 public class CommonAction {
 3     
 4     
 5     @Autowired(required = false)
 6     private CommonService commonService;
 7     
 8     @RequestMapping("getPage")
 9     public @ResponseBody Object getPage(String name) {
10         //"PltmPubNews"
11         return commonService.findPageList(name);
12     }
13 
14 }

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

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

原文地址:https://www.cnblogs.com/taozhiye/p/6594475.html