<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    中文JAVA技術平等自由協作創造

    Java專題文章博客和開源

    常用鏈接

    統計

    最新評論

    基于反射機制的服務代理調用

      實現原理:通過傳遞服務bean的名稱、執行的方法及參數,通過反射機制進行調用返回sat答案

      優點:只需對外提供一個接口服務即可,只要容器中操作服務bean,通過接口即可調用,增加服務bean無需增加對外接口。

      代碼如下:

      接口類

      public interface ProxyService {

      /**

      * webservice調用代理

      * @param beanName bean或類名

      * @param functionName 調用的函數名

      * @param params 參數

      * @return

      * @throws Exception

      */

      Object proxy(String beanName, String functionName,String… params) throws Exception;

      }

      實現類:

      服務基于spring,為了方便獲取服務bean,實現類實現spring的ApplicationContextAware接口

      @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.");

      }

      //獲取服務bean

      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 {

      //獲取服務bean方法

      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 {

      //獲取服務bean方法

      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;

      }

      /**

      * 獲取bean對象

      * @param beanName

      * @return

      */

      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等),對外為的代理調用接口。只需實現一個對外接口,調用服務內部多個業務服務托福答案

     

    posted on 2014-01-12 22:01 好不容易 閱讀(136) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    PK10開獎 PK10開獎
    主站蜘蛛池模板: 边摸边吃奶边做爽免费视频99| 亚洲人成影院午夜网站| 思思久久99热免费精品6| 午夜毛片不卡免费观看视频| 精品亚洲AV无码一区二区三区 | 成年人视频免费在线观看| 亚洲AV本道一区二区三区四区| 日本免费在线观看| 亚洲色图校园春色| 国产a视频精品免费观看| 亚洲已满18点击进入在线观看| 成人au免费视频影院| 香港经典a毛片免费观看看| 亚洲色无码一区二区三区| 免费无码成人AV在线播放不卡| 亚洲综合自拍成人| 一二三四视频在线观看中文版免费 | 亚洲av无码av制服另类专区| 久久国产乱子伦精品免费看| 亚洲毛片基地日韩毛片基地| 国产免费毛不卡片| 水蜜桃视频在线观看免费| 国产AV无码专区亚洲AVJULIA| 99久久人妻精品免费一区| 亚洲香蕉久久一区二区| 国产精品黄页在线播放免费| 精品一区二区三区免费观看 | 亚洲AV无码一区二区三区在线| 无人在线观看完整免费版视频| 国产精品亚洲一区二区三区| 亚洲精品无码mv在线观看网站| 国产成人精品免费视频动漫| 国产亚洲一卡2卡3卡4卡新区 | 免费精品久久久久久中文字幕| 亚洲精品天天影视综合网| 成人人免费夜夜视频观看| jizz免费一区二区三区| 亚洲日韩在线视频| 亚洲天堂免费在线视频| 中字幕视频在线永久在线观看免费| 曰批免费视频播放免费|