使用java动态代理机制实现AOP是丑陋的
关键字: aop但是我们在程序里面生成代码,似乎是一件比较奇怪的事情。就好像我们在干jvm干的活。感觉有些怯怯的。想和大家讨论一下。听听大家的意见。另外AOP还有什么实现原理呢?
另外,使用动态代理似乎效率不是很高。
评论
<context:annotation-config /> <aop:aspectj-autoproxy />
@Aspect
public class AppExceptionInterceptor {
/**
* 应用异常消息服务
*/
private AppExceptionMessageService appExceptionMessageService;
/**
* 系统异常消息拦截器
* @param exception
*/
@AfterThrowing(pointcut = "execution(* com.dwr.Dwr*.*(..))", throwing = "exception")
public void afterThrow(Exception exception) {
if (exception instanceof AppBizException) {
AppBizException abe = (AppBizException) exception;
appExceptionMessageService.processMessage(abe);
} else if (exception instanceof AppRTException) {
AppRTException are = (AppRTException) exception;
appExceptionMessageService.processMessage(are);
}
}
public AppExceptionMessageService getAppExceptionMessageService() {
return appExceptionMessageService;
}
public void setAppExceptionMessageService(
AppExceptionMessageService appExceptionMessageService) {
this.appExceptionMessageService = appExceptionMessageService;
}
}
我把代码贴下来:这段代码是实现了InvocationHandler接口的部分。将日志代码放进去了。
public class LogHandler implements InvocationHandler { private Logger logger = Logger.getLogger(this.getClass().getName()); private Object delegate; public LogHandler(Object delegate){ this.delegate = delegate; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object o = null; try { logger.info("method stats..." + method); o = method.invoke(delegate,args); logger.info("method ends..." + method); } catch (Exception e){ logger.info("Exception happends..."); //excetpion handling. } return o; } }
下面是客户端调用代码:
BusinessInterface businessImp = new BusinessObject(); InvocationHandler handler = new LogHandler(businessImp); BusinessInterface proxy = (BusinessInterface) Proxy.newProxyInstance( businessImp.getClass().getClassLoader(), businessImp.getClass().getInterfaces(), handler); proxy.processBusiness();
businessImp是我们的一个业务实现。我为什么说这种方法比较丑陋呢?就是使用了这些和业务无关的接口和Proxy代理类。虽然这些是jdk提供的,但是给我的感觉是我在写一些和我应用无关的代码。当然上面仅仅是一个简单的示例。也可以想一些办法封装这些操作。但是总给我的感觉是有些怪异,所以希望问一下还有其他的什么实现。惭愧。写这些内容的时候,深感自己水平不行,贻笑大方了。不过刚才想想我的这个帖子标题,的确不好,不应该用这种结论式的标题。给人感觉不好。其实我是想和人探讨的。
难道你不会把proxy的获取封装起来,就不用看代理的代码了
bci当然比dynamic proxy要来的更加的好,运行时怎么也比不过编译时或装载时,不过么有免费的午餐,技术难度也不可同日而语。
spring2.5是可以用aonntation来做aop的~~~
如果自己把代理逻辑抽象出来,与业务逻辑分离,你会发觉就象是在做一个容器。既然如此,为什么不用现成的、成熟的容器呢?
aop不适合做日志
我把代码贴下来:这段代码是实现了InvocationHandler接口的部分。将日志代码放进去了。
public class LogHandler implements InvocationHandler {
private Logger logger = Logger.getLogger(this.getClass().getName());
private Object delegate;
public LogHandler(Object delegate){
this.delegate = delegate;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object o = null;
try {
logger.info("method stats..." + method);
o = method.invoke(delegate,args);
logger.info("method ends..." + method);
} catch (Exception e){
logger.info("Exception happends...");
//excetpion handling.
}
return o;
}
}
下面是客户端调用代码:
BusinessInterface businessImp = new BusinessObject(); InvocationHandler handler = new LogHandler(businessImp); BusinessInterface proxy = (BusinessInterface) Proxy.newProxyInstance( businessImp.getClass().getClassLoader(), businessImp.getClass().getInterfaces(), handler); proxy.processBusiness();
businessImp是我们的一个业务实现。我为什么说这种方法比较丑陋呢?就是使用了这些和业务无关的接口和Proxy代理类。虽然这些是jdk提供的,但是给我的感觉是我在写一些和我应用无关的代码。当然上面仅仅是一个简单的示例。也可以想一些办法封装这些操作。但是总给我的感觉是有些怪异,所以希望问一下还有其他的什么实现。惭愧。写这些内容的时候,深感自己水平不行,贻笑大方了。不过刚才想想我的这个帖子标题,的确不好,不应该用这种结论式的标题。给人感觉不好。其实我是想和人探讨的。
直接用动态代理代码是这样写的,但是用spring或者其他什么框架,你只需要写invoke里面的代码,其他那些机械式都是封装好的
我把代码贴下来:这段代码是实现了InvocationHandler接口的部分。将日志代码放进去了。
public class LogHandler implements InvocationHandler {
private Logger logger = Logger.getLogger(this.getClass().getName());
private Object delegate;
public LogHandler(Object delegate){
this.delegate = delegate;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object o = null;
try {
logger.info("method stats..." + method);
o = method.invoke(delegate,args);
logger.info("method ends..." + method);
} catch (Exception e){
logger.info("Exception happends...");
//excetpion handling.
}
return o;
}
}
下面是客户端调用代码:
BusinessInterface businessImp = new BusinessObject(); InvocationHandler handler = new LogHandler(businessImp); BusinessInterface proxy = (BusinessInterface) Proxy.newProxyInstance( businessImp.getClass().getClassLoader(), businessImp.getClass().getInterfaces(), handler); proxy.processBusiness();
businessImp是我们的一个业务实现。我为什么说这种方法比较丑陋呢?就是使用了这些和业务无关的接口和Proxy代理类。虽然这些是jdk提供的,但是给我的感觉是我在写一些和我应用无关的代码。当然上面仅仅是一个简单的示例。也可以想一些办法封装这些操作。但是总给我的感觉是有些怪异,所以希望问一下还有其他的什么实现。惭愧。写这些内容的时候,深感自己水平不行,贻笑大方了。不过刚才想想我的这个帖子标题,的确不好,不应该用这种结论式的标题。给人感觉不好。其实我是想和人探讨的。
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 218 次
- 性别:

- 来自: 西安

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
使用java动态代理机制实现 ...
利用SPRING2.5的ANNOTATION方式实现AOP,这个够不够优雅? ...
-- by kv0002 -
使用java动态代理机制实现 ...
clarkhill 写道 我把代码贴下来:这段代码是实现了Invocati ...
-- by spiritfrog -
不要让开源架构代替我们的 ...
引用我们应该在我们的设计中对引入的框架部分做我们自的接口,这样就可以摆脱框架侵入 ...
-- by lakemove -
不要让开源架构代替我们的 ...
看到有人说我对hi的理解有问题。汗一个。不是我对hibernate的理解有问题。 ...
-- by clarkhill -
不要让开源架构代替我们的 ...
晕倒,我们是产品的公司,我们的产品确实需要在不同数据库上跑,这个是需求。我们怎么 ...
-- by slaser






评论排行榜