2023年java反射机制和动态代理精选
文件格式:DOCX
时间:2023-04-29 00:00:00    小编:轻创业玩家

2023年java反射机制和动态代理精选

小编:轻创业玩家

每个人都曾试图在平淡的学习、工作和生活中写一篇文章。写作是培养人的观察、联想、想象、思维和记忆的重要手段。范文书写有哪些要求呢?我们怎样才能写好一篇范文呢?下面我给大家整理了一些优秀范文,希望能够帮助到大家,我们一起来看一看吧。

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 = ger(getclass());

@resource

private applicationcontext applicationcontext;

@override

public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception {

ationcontext = applicationcontext;

}

/**

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

*/

@suppresswarnings("rawtypes")

@override

public object proxy(string beanname, string functionname, string… params) throws serviceexception {

//参数判断

if(y(beanname)){

throw new exception("error: beanname is empty.");

}

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

("proxy params is empty.");

}

method method = null;

//处理无参数调用

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

try {

//获取服务bean方法

method = ss()。getmethod(functionname);

} catch (securityexception e) {

("proxy getmethod securityexception:"+sage());

tacktrace();

} catch (exception e) {

("proxy invoke illegalargumentexception:"+sage());

tacktrace();

throw new exception("error: get method exception:"+sage());

}

}else{

//处理有参数调用

//处理调用方法参数

class[] paratypes = new class[];

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

paratypes[i] = ;

}

//获取服务bean方法

method = ss()。getmethod(functionname, paratypes);

}catch (exception e) {

("proxy invoke illegalargumentexception:"+sage());

tacktrace();

throw new exception("error: get method exception:"+sage());

}

}

if(method == null ){

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

}

object rs = null;

try {

//调用返回数据

rs = (bean,params);

} catch (exception e) {

("proxy invoke illegalargumentexception:"+sage());

tacktrace();

throw new exception("error: invoke method exception:"+sage());

}

return rs;

}

/**

* 获取bean对象

* @param beanname

* @return

*/

private object getbean(string beanname){

object bean = null;

bean = n(beanname);

if(bean == null){

try {

class classe = e(beanname);

bean = tance();

} catch (instantiationexception e) {

("getbean instantiationexception:"+sage());

tacktrace();

} catch (illegalaccessexception e) {

("getbean illegalaccessexception:"+sage());

tacktrace();

}catch ( classnotfoundexception e) {

("getbean classnotfoundexception:"+sage());

猜你喜欢 网友关注 本周热点 软件
musicolet
2025-08-21
BBC英语
2025-08-21
百度汉语词典
2025-08-21
精选文章
基于你的浏览为你整理资料合集
复制