java AspectJ:两种教程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11761386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:16:40  来源:igfitidea点击:

AspectJ: two kinds of tutorials

javaaspectj

提问by alicjasalamon

From my research I know there are two ways of using AspectJ. First is by creating A.ajclass and second by adding annotation @Aspectin A.java.

根据我的研究,我知道有两种使用 AspectJ 的方法。首先是创建A.aj类,其次是@AspectA.java.

I was looking for a good tutorial for this second kind, especially about lines like

我正在为第二种类型寻找一个很好的教程,尤其是关于像这样的行

@After("call(void fooMethod())")  
@Around("call(void sendAndReceive())") 
@Before("execution(String greeting(..)) && args(context)")

but I don't know how they are called.

但我不知道他们是怎么称呼的。

Could you recommend some tutorials?

你能推荐一些教程吗?

回答by Tomasz Nurkiewicz

This style is called @AspectJto emphasize the role of annotations. Have a look at official docsand @AspectJ cheat sheet.

这种风格被称为@AspectJ,以强调注解的作用。查看官方文档@AspectJ 备忘单

回答by Vikram

Annotation and the XML ways:

注释和 XML 方式:

Annotation way:Minimal xml Config file:

注解方式:最小xml配置文件:

<!-- Enable autoproxy to pick up all Java files tagged as @Aspect behave like Aspects -->
<aspectj-autoproxy/>
<!-- define bean -->
<!-- Note: MyUselessAspect.java should exist and this class must be tagged as @Aspect -->
<bean id="myUselessAspect" class="...MyUselessAspect" />

XML way:Minimal XML configuration:

XML方式:最小的XML配置:

<aop:config>
   <aop:aspect ref="myUselessAspect">
        <!-- this point-cut picks all methods of any return type, from any package/class with any number of Parameters -->
    <aop:before method="doSomethingBeforeMethodCall" pointcut="execution(* *.*(..))"/>
    <aop:after method="doSomethingAfterMethodCall" pointcut="execution(* *.*(..))"/>
   </aop:aspect>        
</aop:config>
<!-- No need to Annotate this java Class as @Aspect. Neither you need to define any
 Point-cuts or Advices in the Java file. The <aop:config> tag takes care of everything -->
<bean id="myUselessAspect" class="...MyUselessAspect"></bean>

No changes in code required.

无需更改代码。

Pre-Req:aop Namespace must exist in the XML file

先决条件:aop 命名空间必须存在于 XML 文件中