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

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

优点:只需对外提供一个接口服务即可,只要容器中操作服务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等),对外为的代理调用接口。只需实现一个对外接口,调用服务内部多个业务服务。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值