註釋和XML方式:
註釋方式: 最小的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方式: 最少的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>
無需更改代碼。
預-REQ: AOP命名空間必須存在於XML文件
注意到了人們使用AspectJ中受益的,但不熟悉的春天 - 在這裏維克拉姆的回答是解決如何在Spring的配置配置方面文件。 另外,他提供了第三種方法來定義一個方面 - 通過XML配置文件。當您直接在AspectJ中進行編程時,AspectJ配置文件(通常稱爲「aop.xml」)中提供了類似的功能,而不是Spring。 不幸的是,這並沒有解決原來的問題。 – 2013-06-20 15:58:23