java - spring AOP 不生效
問題描述
寫了個切面, 如果切點定義聲明在Controller上面的方法,這對應的通知能夠執行, 如果不是Controller直接調用的則通知無法執行.
切面聲明:
@Aspect@Componentpublic class SessionAspect { @Pointcut('execution(* cn.test.service.impl.ShopServiceImpl.myShops(..))') private void myShops() { }@Pointcut('execution(* cn.test.service.impl.ShopServiceImpl.test(..))') private void test() { } @Before('myShops()') public void doBefore() {System.out.println('hello'); }@Before('test()') public void doBefore() {System.out.println('test'); }}
controller 的方法
@RequestMapping(value = '/my', method = RequestMethod.GET)public Object myShops(String userSid, ModelMap result) { return this.shopService.myShops(userSid);}
因為myShops在controller中直接調用, 通知能夠觸發執行, 打印出hello, 而test方法沒有在controller中顯示調用, 所有即便執行了test方法也不會通知也沒有被觸發執行.基于Spring MVC.
問題解答
回答1:Spring AOP 只對 Bean 進行代理,如果你的實例不是從 Spring 獲取來的 Bean 而是自己實例出來的它是沒法進行代理的。
相關文章:
1. php工具中的mysql還是5.1以下的,請問如何才能升級到5.1以上?2. apache - 想把之前寫的單機版 windows 軟件改成網絡版,讓每個用戶可以注冊并登錄。類似 qq 的登陸,怎么架設服務器呢?3. java - 根據月份查詢多個表里的內容怎么實現好?4. javascript - 在 model里定義的 引用表模型時,model為undefined。5. atom開始輸入!然后按tab只有空格出現沒有html格式出現6. css3 - 這個右下角折角用css怎么畫出來?7. css3 - 沒明白盒子的height隨width的變化這段css是怎樣實現的?8. python3.x - c++調用python39. javascript - 移動端自適應10. ios - 類似微博首頁,一張圖的時候是如何確定圖大小的?
