2016年JAVA认证基础知识:基于反射机制的服务代理调用

在不断注重高考能力提高的同时,尤其是JAVA认证备考的后阶段,我们选择的是求准求稳求规范。此复习辅导不等于题海战术,而是要积累实战经验,解决掉一些考场失误等问题。下面一起来看看JAVA认证基础知识——基于反射机制的服务代理调用,相信对大家学习java有所帮助!

2016年JAVA认证基础知识:基于反射机制的服务代理调用

实现原理:通过传递服务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 = ogger(getClass());

@Resource

private ApplicationContext applicationContext;

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

icationContext = applicationContext;

}

/**

* 通过代理执行业务方法,方法数据

*/

@SuppressWarnings("rawtypes")

@Override

public Object proxy(String beanName, String functionName, String… params) throws ServiceException {

//参数判断

if(pty(beanName)){

throw new Exception("error: beanName is empty.");

}

if(pty(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 || th ==0){

("proxy params is empty.");

}

Method method = null;

//处理无参数调用

if(params == null || th ==0){

try {

//获取服务bean方法

method = lass()。getMethod(functionName);

} catch (SecurityException e) {

r("proxy getMethod SecurityException:"+essage());

tStackTrace();

} catch (Exception e) {

r("proxy invoke IllegalArgumentException:"+essage());

tStackTrace();

throw new Exception("error: get method Exception:"+essage());

}

}else{

//处理有参数调用

//处理调用方法参数

Class[] paraTypes = new Class[th];

for (int i = 0; i < th; i++) {

paraTypes[i] = s;

}

//获取服务bean方法

method = lass()。getMethod(functionName, paraTypes);

}catch (Exception e) {

r("proxy invoke IllegalArgumentException:"+essage());

tStackTrace();

throw new Exception("error: get method Exception:"+essage());

}

}

if(method == null ){

throw new Exception("error: function is not exist.");

}

Object rs = null;

try {

//调用返回数据

rs = ke(bean,params);

} catch (Exception e) {

r("proxy invoke IllegalArgumentException:"+essage());

tStackTrace();

throw new Exception("error: invoke method Exception:"+essage());

}

return rs;

}

/**

* 获取bean对象

* @param beanName

* @return

*/

private Object getBean(String beanName){

Object bean = null;

bean = ean(beanName);

if(bean == null){

try {

Class classe = ame(beanName);

bean = nstance();

} catch (InstantiationException e) {

r("getBean InstantiationException:"+essage());

tStackTrace();

} catch (IllegalAccessException e) {

r("getBean IllegalAccessException:"+essage());

tStackTrace();

}catch ( ClassNotFoundException e) {

r("getBean ClassNotFoundException:"+essage());

tStackTrace();

}

}

g("getBean(),beanName:"+beanName);

return bean;

}

}

调用方式如下:

y("testservice","say","helloword");

testservice 为spring中bean实例

say 为testservice的业务方法

helloword 为参数

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