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. angular.js - angular里的ui-view里,獲取當前頁面的狀態參數用$state.params,在vue里類似的語法是什么呢?2. 前端 - CSS3問題:請問-webkit-background-clip屬性,需要寫在background屬性后面嗎?3. javascript - 按鈕鏈接到另一個網址 怎么通過百度統計計算按鈕的點擊數量4. html5 - h5+中webview的show方法有延遲5. 微信開放平臺 - ios APP能不能打開微信然后通過微信跳轉到指定的URL?6. 淺談vue生命周期共有幾個階段?分別是什么?7. node.js - nodejs+express+vue8. Mysql啟動發生系統錯誤10679. nginx - vue-cli生成的項目打包發到服務器后怎么代理api?10. 大家好,我想請問一下怎么做搜索欄能夠搜索到自己網站的內容。
