當前位置:才華齋>IT認證>JAVA認證>

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

JAVA認證 閲讀(2.05W)

在不斷注重大學聯考能力提高的同時,尤其是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等),對外為的代理調用接口。只需實現一個對外接口,調用服務內部多個業務服務。