基于反射机制的服务代理调用

实现原理:通过传递服务bean的名称、执行的方法及参数,通过反射机制进行调用返回。

优点:只需对外提供一个接口服务即可,只要容器中操作服务bean,通过接口即可调用,增加服务bean无需增加对外接口。

代码如下:

接口类

 

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. public interface ProxyService {  
  2.     /** 
  3.      * webservice调用代理 
  4.      * @param beanName  bean或类名 
  5.      * @param functionName 调用的函数名 
  6.      * @param params 参数 
  7.      * @return 
  8.      * @throws Exception 
  9.      */  
  10.     Object proxy(String beanName, String functionName,String... params) throws Exception;  
  11. }  

实现类:

 

服务基于spring,为了方便获取服务bean,实现类实现spring的ApplicationContextAware接口

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. @Service  
  2. public class ProxyServiceImpl implements ProxyService ,ApplicationContextAware{  
  3.   
  4.     protected final Logger logger = LoggerFactory.getLogger(getClass());  
  5.       
  6.     @Resource  
  7.     private ApplicationContext applicationContext;  
  8.   
  9.     @Override  
  10.     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
  11.         this.applicationContext = applicationContext;  
  12.     }  
  13.       
  14.     /** 
  15.      * 通过代理执行业务方法,方法数据 
  16.      */  
  17.     @SuppressWarnings("rawtypes")  
  18.     @Override  
  19.     public Object proxy(String beanName, String functionName, String... params) throws ServiceException {  
  20.   
  21.          //参数判断  
  22.          if(StringUtils.isEmpty(beanName)){  
  23.              throw new Exception("error: beanName is empty.");  
  24.          }  
  25.          if(StringUtils.isEmpty(functionName)){  
  26.              throw new Exception("error: functionName is empty.");  
  27.          }  
  28.          //获取服务bean  
  29.          Object bean = getBean(beanName);  
  30.          if(bean == null){  
  31.              throw new Exception("error: bean is not exist.");  
  32.          }  
  33.         if(params == null || params.length ==0){  
  34.             logger.warn("proxy  params is empty.");  
  35.         }  
  36.           
  37.         Method method = null;  
  38.         //处理无参数调用  
  39.         if(params == null || params.length ==0){  
  40.              try {  
  41.                 //获取服务bean方法  
  42.                 method = bean.getClass().getMethod(functionName);  
  43.             } catch (SecurityException e) {  
  44.                 logger.error("proxy getMethod SecurityException:"+e.getMessage());  
  45.                 e.printStackTrace();  
  46.             } catch (Exception e) {  
  47.                 logger.error("proxy invoke IllegalArgumentException:"+e.getMessage());  
  48.                 e.printStackTrace();  
  49.                 throw new Exception("error: get method Exception:"+e.getMessage());  
  50.             }   
  51.         }else{  
  52.               //处理有参数调用  
  53.               //处理调用方法参数  
  54.               Class[] paraTypes = new Class[params.length];  
  55.               for (int i = 0; i < paraTypes.length; i++) {  
  56.                 paraTypes[i] = String.class;  
  57.               }  
  58.             try {  
  59.                 //获取服务bean方法  
  60.                 method = bean.getClass().getMethod(functionName, paraTypes);  
  61.             }catch (Exception e) {  
  62.                 logger.error("proxy invoke IllegalArgumentException:"+e.getMessage());  
  63.                 e.printStackTrace();  
  64.                 throw new Exception("error: get method Exception:"+e.getMessage());  
  65.             }   
  66.         }  
  67.         if(method == null ){  
  68.             throw new Exception("error: function is not exist.");  
  69.         }  
  70.            
  71.          Object rs = null;  
  72.         try {  
  73.             //调用返回数据  
  74.             rs = method.invoke(bean,params);  
  75.         } catch (Exception e) {  
  76.             logger.error("proxy invoke IllegalArgumentException:"+e.getMessage());  
  77.             e.printStackTrace();  
  78.             throw new Exception("error: invoke method Exception:"+e.getMessage());  
  79.               
  80.         }   
  81.         return rs;  
  82.     }  
  83.       
  84.     /** 
  85.      * 获取bean对象 
  86.      * @param beanName 
  87.      * @return 
  88.      */  
  89.     private Object getBean(String beanName){  
  90.         Object bean = null;  
  91.         bean = applicationContext.getBean(beanName);  
  92.         if(bean == null){  
  93.             try {  
  94.                 Class<?> classe = Class.forName(beanName);  
  95.                 bean = classe.newInstance();  
  96.             } catch (InstantiationException e) {  
  97.                 logger.error("getBean InstantiationException:"+e.getMessage());  
  98.                 e.printStackTrace();  
  99.             } catch (IllegalAccessException e) {  
  100.                 logger.error("getBean IllegalAccessException:"+e.getMessage());  
  101.                 e.printStackTrace();  
  102.             }catch ( ClassNotFoundException e) {  
  103.                 logger.error("getBean ClassNotFoundException:"+e.getMessage());  
  104.                 e.printStackTrace();  
  105.             }  
  106.         }  
  107.         logger.debug("getBean(),beanName:"+beanName);  
  108.         return bean;  
  109.     }  
  110.   
  111. }  

 

调用方式如下:

proxyService.proxy("testservice","say","helloword");
testservice 为spring中bean实例
say 为testservice的业务方法
helloword 为参数

以上方式可以使用与远程调用(如webservice等),对外为的代理调用接口。只需实现一个对外接口,调用服务内部多个业务服务。

转载自:http://blog.csdn.net/zhu_tianwei/article/details/18082045,如涉及版权问题,请联系我。本人即刻删除该转载贴

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值