反射和动态加载bean

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

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

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

strip

1importorg.slf4j.Logger;2importorg.slf4j.LoggerFactory;3importorg.springframework.beans.factory.DisposableBean;4importorg.springframework.context.ApplicationContext;5importorg.springframework.context.ApplicationContextAware;6importorg.springframework.stereotype.Service;78/**9* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.10*/11@Service12publicclassSpringContextHolderimplementsApplicationContextAware, DisposableBean {1314privatestaticApplicationContext applicationContext =null;1516privatestaticLogger logger = LoggerFactory.getLogger(SpringContextHolder.class);1718/**19* 实现ApplicationContextAware接口, 注入Context到静态变量中.20*/21publicvoidsetApplicationContext(ApplicationContext applicationContext) {22logger.debug("注入ApplicationContext到SpringContextHolder:" +applicationContext);2324if(SpringContextHolder.applicationContext !=null) {25logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:"26+SpringContextHolder.applicationContext);27}2829SpringContextHolder.applicationContext = applicationContext;//NOSONAR30}3132/**33* 实现DisposableBean接口,在Context关闭时清理静态变量.34*/35@Override36publicvoiddestroy()throwsException {37SpringContextHolder.clear();38}3940/**41* 取得存储在静态变量中的ApplicationContext.42*/43publicstaticApplicationContext getApplicationContext() {44assertContextInjected();45returnapplicationContext;46}4748/**49* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.50*/51@SuppressWarnings("unchecked")52publicstaticT getBean(String name) {53assertContextInjected();54return(T) applicationContext.getBean(name);55}5657/**58* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.59*/60publicstatic T getBean(Cla***equiredType) {61assertContextInjected();62returnapplicationContext.getBean(requiredType);63}6465/**66* 清除SpringContextHolder中的ApplicationContext为Null.67*/68publicstaticvoidclear() {69logger.debug("清除SpringContextHolder中的ApplicationContext:" +applicationContext);70applicationContext =null;71}7273/**74* 检查ApplicationContext不为空.75*/76privatestaticvoidassertContextInjected() {77if(applicationContext ==null) {78thrownewIllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");79}80}81}

strip

之后是service层代码

strip

1publicinterfaceCommonService {23/**4* 分页查询5*@paramobject  表名6*@returnPage<>  列表7*/8Object findPageList(String name);9}

strip

strip

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

strip

controller

strip

1@Controller2publicclassCommonAction {345@Autowired(required =false)6privateCommonService commonService;78@RequestMapping("getPage")9public@ResponseBody Object getPage(String name) {10//"PltmPubNews"11returncommonService.findPageList(name);12}1314}

strip

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

欢迎加入技术QQ群:364595326



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章