• <sub id="h4knl"><ol id="h4knl"></ol></sub>
    <sup id="h4knl"></sup>
      <sub id="h4knl"></sub>

      <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
      1. <strong id="h4knl"></strong>

      2. JAVA認證基礎知識:基于反射機制的服務代理調用

        時間:2024-09-20 00:55:41 JAVA認證 我要投稿

        JAVA認證基礎知識:基于反射機制的服務代理調用

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

        JAVA認證基礎知識:基于反射機制的服務代理調用

          優點:只需對外提供一個接口服務即可,只要容器中操作服務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;

          }

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

        《&.doc》
        将本文的Word文档下载到电脑,方便收藏和打印
        推荐度:
        点击下载文档

        【JAVA認證基礎知識:基于反射機制的服務代理調用】相關文章:

        2016年JAVA認證基礎知識:基于反射機制的服務代理調用03-03

        JAVA認證基礎知識:Java獲取當前的系統時間03-18

        JAVA認證基礎知識:JavaNativeInterface學習小結01-11

        java調用cmd命令01-29

        Java認證基礎知識:java字符串轉化整型問題03-18

        JAVA認證簡介03-19

        關于Java的內省與反射03-17

        JAVA認證基礎知識:JSP使用數據庫操作03-18

        SUN JAVA認證介紹12-18

        在线咨询
        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码
      3. <sub id="h4knl"><ol id="h4knl"></ol></sub>
        <sup id="h4knl"></sup>
          <sub id="h4knl"></sub>

          <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
          1. <strong id="h4knl"></strong>

          2. 日本区一视频区二视频 | 欧美日韩在线视频 | 在线观看91精品国产免费 | 亚洲日本va中文字幕久久 | 在线看片免费人成视频久试看 | 在线精品视频一区二区三区 |

            JAVA認證基礎知識:基于反射機制的服務代理調用

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

            JAVA認證基礎知識:基于反射機制的服務代理調用

              優點:只需對外提供一個接口服務即可,只要容器中操作服務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;

              }

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