生活随笔
收集整理的這篇文章主要介紹了
基于反射机制的服务代理调用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載自http://blog.csdn.net/zhu_tianwei/article/details/18082045
實現原理:通過傳遞服務bean的名稱、執行的方法及參數,通過反射機制進行調用返回。
優點:只需對外提供一個接口服務即可,只要容器中操作服務bean,通過接口即可調用,增加服務bean無需增加對外接口。
代碼如下:
接口類
[java]?view plain
?copy public?interface?ProxyService?{?? ????? ? ? ? ? ? ? ?? ????Object?proxy(String?beanName,?String?functionName,String...?params)?throws?Exception;?? }??
實現類:
服務基于spring,為了方便獲取服務bean,實現類實現spring的ApplicationContextAware接口
[java]?view plain
?copy @Service?? public?class?ProxyServiceImpl?implements?ProxyService?,ApplicationContextAware{?? ?? ????protected?final?Logger?logger?=?LoggerFactory.getLogger(getClass());?? ?????? ????@Resource?? ????private?ApplicationContext?applicationContext;?? ?? ????@Override?? ????public?void?setApplicationContext(ApplicationContext?applicationContext)?throws?BeansException?{?? ????????this.applicationContext?=?applicationContext;?? ????}?? ?????? ????? ? ?? ????@SuppressWarnings("rawtypes")?? ????@Override?? ????public?Object?proxy(String?beanName,?String?functionName,?String...?params)?throws?ServiceException?{?? ?? ??????????? ?????????if(StringUtils.isEmpty(beanName)){?? ?????????????throw?new?Exception("error:?beanName?is?empty.");?? ?????????}?? ?????????if(StringUtils.isEmpty(functionName)){?? ?????????????throw?new?Exception("error:?functionName?is?empty.");?? ?????????}?? ??????????? ?????????Object?bean?=?getBean(beanName);?? ?????????if(bean?==?null){?? ?????????????throw?new?Exception("error:?bean?is?not?exist.");?? ?????????}?? ????????if(params?==?null?||?params.length?==0){?? ????????????logger.warn("proxy??params?is?empty.");?? ????????}?? ?????????? ????????Method?method?=?null;?? ?????????? ????????if(params?==?null?||?params.length?==0){?? ?????????????try?{?? ?????????????????? ????????????????method?=?bean.getClass().getMethod(functionName);?? ????????????}?catch?(SecurityException?e)?{?? ????????????????logger.error("proxy?getMethod?SecurityException:"+e.getMessage());?? ????????????????e.printStackTrace();?? ????????????}?catch?(Exception?e)?{?? ????????????????logger.error("proxy?invoke?IllegalArgumentException:"+e.getMessage());?? ????????????????e.printStackTrace();?? ????????????????throw?new?Exception("error:?get?method?Exception:"+e.getMessage());?? ????????????}??? ????????}else{?? ???????????????? ???????????????? ??????????????Class[]?paraTypes?=?new?Class[params.length];?? ??????????????for?(int?i?=?0;?i?<?paraTypes.length;?i++)?{?? ????????????????paraTypes[i]?=?String.class;?? ??????????????}?? ????????????try?{?? ?????????????????? ????????????????method?=?bean.getClass().getMethod(functionName,?paraTypes);?? ????????????}catch?(Exception?e)?{?? ????????????????logger.error("proxy?invoke?IllegalArgumentException:"+e.getMessage());?? ????????????????e.printStackTrace();?? ????????????????throw?new?Exception("error:?get?method?Exception:"+e.getMessage());?? ????????????}??? ????????}?? ????????if(method?==?null?){?? ????????????throw?new?Exception("error:?function?is?not?exist.");?? ????????}?? ??????????? ?????????Object?rs?=?null;?? ????????try?{?? ?????????????? ????????????rs?=?method.invoke(bean,params);?? ????????}?catch?(Exception?e)?{?? ????????????logger.error("proxy?invoke?IllegalArgumentException:"+e.getMessage());?? ????????????e.printStackTrace();?? ????????????throw?new?Exception("error:?invoke?method?Exception:"+e.getMessage());?? ?????????????? ????????}??? ????????return?rs;?? ????}?? ?????? ????? ? ? ? ?? ????private?Object?getBean(String?beanName){?? ????????Object?bean?=?null;?? ????????bean?=?applicationContext.getBean(beanName);?? ????????if(bean?==?null){?? ????????????try?{?? ????????????????Class<?>?classe?=?Class.forName(beanName);?? ????????????????bean?=?classe.newInstance();?? ????????????}?catch?(InstantiationException?e)?{?? ????????????????logger.error("getBean?InstantiationException:"+e.getMessage());?? ????????????????e.printStackTrace();?? ????????????}?catch?(IllegalAccessException?e)?{?? ????????????????logger.error("getBean?IllegalAccessException:"+e.getMessage());?? ????????????????e.printStackTrace();?? ????????????}catch?(?ClassNotFoundException?e)?{?? ????????????????logger.error("getBean?ClassNotFoundException:"+e.getMessage());?? ????????????????e.printStackTrace();?? ????????????}?? ????????}?? ????????logger.debug("getBean(),beanName:"+beanName);?? ????????return?bean;?? ????}?? ?? }??
調用方式如下:
proxyService.proxy("testservice","say","helloword");
testservice 為spring中bean實例
say 為testservice的業務方法
helloword 為參數
以上方式可以使用與遠程調用(如webservice等),對外為的代理調用接口。只需實現一個對外接口,調用服務內部多個業務服務。
總結
以上是生活随笔為你收集整理的基于反射机制的服务代理调用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。