Aspectj:从外部jar拦截方法 [英] Aspectj: intercept method from external jar

查看:467
本文介绍了Aspectj:从外部jar拦截方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用X.jar并将其添加到我的AspectJ项目(在Eclipse中).我在X.jar中为方法myMethod()编写了切入点和建议.

I am using a X.jar and adding to my AspectJ project(in eclipse). I have written pointcut and advice for a method myMethod() inside X.jar.

但是Aspectj并未拦截此方法调用.

But aspectj is not intercepting this method call.

如何告诉Aspectj拦截外部jar上的方法调用?还是不可能?

How can I tell aspectj to intercept method calls on external jars.Or is it not possible?

谢谢

推荐答案

有两个选项:

a)将方面编译成JAR
b)使用加载时间编织(我会选择那个)

a) compile the aspects into the JAR
b) use load time weaving (I'd go with that one)

这两个都是高级主题,建议您阅读Ramnivas Laddad的《 AspectJ in Action》(第二版) 了解更多信息.

Both of these are advanced topics, I'd suggest you read AspectJ in Action (2nd Ed) by Ramnivas Laddad to learn more.

要澄清:切入点有不同类型.如果您的代码调用了库的方法,那么您当然可以拦截这些调用,因为它们发生在您的代码中.因此,call()切入点将起作用,但是execute()(以及许多其他)切入点将不起作用,因为它们会更改执行方法,该方法不在您的代码库中.因此,您必须更改库的字节码(选项a)或更改将其加载到应用程序中的方式(选项b).

To clarify: there are different types of pointcuts. If your code calls the library's methods, you can of course intercept these calls, as they happen in your code. So call() pointcuts will work, but execute() (and many other) pointcuts won't because they change the executing method, which is not in your code base. So you have to either change the byte code of the library (option a) or change how it is loaded into your application (option b).

这篇关于Aspectj:从外部jar拦截方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆