- 相關(guān)推薦
JAVA認(rèn)證基礎(chǔ)知識(shí):基于反射機(jī)制的服務(wù)代理調(diào)用
實(shí)現(xiàn)原理:通過(guò)傳遞服務(wù)bean的名稱、執(zhí)行的方法及參數(shù),通過(guò)反射機(jī)制進(jìn)行調(diào)用返回。
優(yōu)點(diǎn):只需對(duì)外提供一個(gè)接口服務(wù)即可,只要容器中操作服務(wù)bean,通過(guò)接口即可調(diào)用,增加服務(wù)bean無(wú)需增加對(duì)外接口。
代碼如下:
接口類
public interface ProxyService {
/**
* webservice調(diào)用代理
* @param beanName bean或類名
* @param functionName 調(diào)用的函數(shù)名
* @param params 參數(shù)
* @return
* @throws Exception
*/
Object proxy(String beanName, String functionName,String… params) throws Exception;
}
實(shí)現(xiàn)類:
服務(wù)基于spring,為了方便獲取服務(wù)bean,實(shí)現(xiàn)類實(shí)現(xiàn)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;
}
/**
* 通過(guò)代理執(zhí)行業(yè)務(wù)方法,方法數(shù)據(jù)
*/
@SuppressWarnings("rawtypes")
@Override
public Object proxy(String beanName, String functionName, String… params) throws ServiceException {
//參數(shù)判斷
if(StringUtils.isEmpty(beanName)){
throw new Exception("error: beanName is empty.");
}
if(StringUtils.isEmpty(functionName)){
throw new Exception("error: functionName is empty.");
}
//獲取服務(wù)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;
//處理無(wú)參數(shù)調(diào)用
if(params == null || params.length ==0){
try {
//獲取服務(wù)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{
//處理有參數(shù)調(diào)用
//處理調(diào)用方法參數(shù)
Class[] paraTypes = new Class[params.length];
for (int i = 0; i < paraTypes.length; i++) {
paraTypes[i] = String.class;
}
//獲取服務(wù)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 {
//調(diào)用返回?cái)?shù)據(jù)
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對(duì)象
* @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;
}
}
調(diào)用方式如下:
proxyService.proxy("testservice","say","helloword");
testservice 為spring中bean實(shí)例
say 為testservice的業(yè)務(wù)方法
helloword 為參數(shù)
以上方式可以使用與遠(yuǎn)程調(diào)用(如webservice等),對(duì)外為的代理調(diào)用接口。只需實(shí)現(xiàn)一個(gè)對(duì)外接口,調(diào)用服務(wù)內(nèi)部多個(gè)業(yè)務(wù)服務(wù)。
【JAVA認(rèn)證基礎(chǔ)知識(shí):基于反射機(jī)制的服務(wù)代理調(diào)用】相關(guān)文章:
JAVA認(rèn)證基礎(chǔ)知識(shí):JavaNativeInterface學(xué)習(xí)小結(jié)06-02
java調(diào)用cmd命令01-29
Java的基礎(chǔ)知識(shí)07-27
Java基礎(chǔ)知識(shí)詳解12-07
Java語(yǔ)言的特點(diǎn)和實(shí)現(xiàn)機(jī)制08-13
java認(rèn)證考試試題及答案08-20
2016年Java認(rèn)證考試題08-26