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. python - flask sqlalchemy signals 無法觸發2. 為什么python中實例檢查推薦使用isinstance而不是type?3. mysql里的大表用mycat做水平拆分,是不是要先手動分好,再配置mycat4. window下mysql中文亂碼怎么解決??5. python - 獲取到的數據生成新的mysql表6. python - (初學者)代碼運行不起來,求指導,謝謝!7. sass - gem install compass 使用淘寶 Ruby 安裝失敗,出現 4048. linux - 【已解決】fabric部署的Python項目Apache啟動之后提示403Forbidden該如何解決?9. python的文件讀寫問題?10. javascript - js 對中文進行MD5加密和python結果不一樣。
