AspectJ: two kinds of tutorials

天涯浪子 提交于 2019-12-05 02:22:14

This style is called @AspectJ to emphasize the role of annotations. Have a look at official docs and @AspectJ cheat sheet.

Annotation and the XML ways:

Annotation way: Minimal xml Config file:

<!-- 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:

<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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!