這聽起來像你正在嘗試Spring AOP和AspectJ之間做出選擇,但你假設Spring AOP需要基於XML的配置。它沒有。您可以使用AspectJ的註解兩個Spring AOP和AspectJ:
package com.example.app;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
@Aspect
public class NotificationAspect {
@Autowired private NotificationGateway notificationGateway;
@Pointcut("execution(* com.example.app.ItemDeleter.delete(com.example.app.Item))")
private void deleteItemOps() { }
@AfterReturning(pointcut = "deleteItemOps() && args(item)")
public void notifyDelete(Item item) {
notificationGateway.notify(item, ConfigManagementEvent.OP_DELETE);
}
}
所以,如果你想比較Spring AOP和AspectJ,它更明智的AspectJ的比較基於註解的Spring AOP的。 Spring AOP通常比較簡單(你不需要AspectJ編譯器)。因此參考文檔推薦使用Spring AOP而不是AspectJ,除非您需要更多異國情調的切入點。
UPDATE:應對下面的OP的評論,我們可以使用XML配置奉勸具體方法:
<aop:config>
<aop:pointcut
id="deleteItemOps"
expression="execution(* com.example.app.ItemDeleter.delete(com.example.app.Item))" />
<aop:advisor
advice-ref="notificationAdvice"
pointcut-ref="deleteItemOps() && args(item)" />
</aop:config>
如果你要嵌入的切入點就在<aop:advisor>
,你也可以這樣做:
<aop:config>
<aop:advisor
advice-ref="notificationAdvice"
pointcut="execution(* com.example.app.ItemDeleter.delete(com.example.app.Item)) && args(item)" />
</aop:config>
(我沒有檢查XML配置的&& args(item)
一部分,但我想這是因爲我給的例子確定。如果它不工作,嘗試將其移除並隨時編輯日)
是的我想我是問使用spring aop與xml配置相比,使用spring aop與aspectj註解,使用它與aspectj註解似乎允許我們將我們的方面應用到單獨的方法/連接點而使用xml配置意味着我們將它應用到切入點表達式中包含的一個類或一組方法中 - spring與aspectj似乎在這方面提供了更多的靈活性? – Shane 2014-10-12 00:53:30
是的,我認爲這可能是你的意圖。考慮更新標題以反映這一點。無論如何,我更新了回覆。 – 2014-10-12 01:06:06
謝謝,會做,我懷疑上述解決方案將需要一個不同的顧問標籤爲每個切入點/方法的建議將被應用? – Shane 2014-10-12 01:20:37