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. html5 - 在一個頁面中 初始了兩個swiper 不知道哪里錯了 一直不對2. html5和Flash對抗是什么情況?3. 微信小程序session無法緩存的問題4. 前端 - 微信支付開發:調用jsapi時缺少參數total_fee5. mac連接阿里云docker集群,已經卡了2天了,求問?6. docker綁定了nginx端口 外部訪問不到7. phpadmin的數據庫,可以設置自動變化時間的變量嗎?就是不需要接收時間數據,自動變化8. node.js - vue怎么部署到網站里9. java - Web開發 - POI導出帶有下拉框的Excel和解決下拉中數組過多而產生的異常10. node.js - 如何在vue模板中使用nodeJS?
