java - spring AOP 不生效
問題描述
寫了個(gè)切面, 如果切點(diǎn)定義聲明在Controller上面的方法,這對(duì)應(yīng)的通知能夠執(zhí)行, 如果不是Controller直接調(diào)用的則通知無法執(zhí)行.
切面聲明:
@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);}
因?yàn)閙yShops在controller中直接調(diào)用, 通知能夠觸發(fā)執(zhí)行, 打印出hello, 而test方法沒有在controller中顯示調(diào)用, 所有即便執(zhí)行了test方法也不會(huì)通知也沒有被觸發(fā)執(zhí)行.基于Spring MVC.
問題解答
回答1:Spring AOP 只對(duì) Bean 進(jìn)行代理,如果你的實(shí)例不是從 Spring 獲取來的 Bean 而是自己實(shí)例出來的它是沒法進(jìn)行代理的。
相關(guān)文章:
1. node.js - 在vuejs-templates/webpack中dev-server.js里為什么要exports readyPromise?2. mysql優(yōu)化 - 關(guān)于mysql分區(qū)3. html5 - 如何實(shí)現(xiàn)帶陰影的不規(guī)則容器?4. javascript - 循環(huán)嵌套多個(gè)promise應(yīng)該如何實(shí)現(xiàn)?5. python - 管道符和ssh傳文件6. 請(qǐng)教各位大佬,瀏覽器點(diǎn) 提交實(shí)例為什么沒有反應(yīng)7. objective-c - iOS開發(fā)支付寶和微信支付完成為什么跳轉(zhuǎn)到了之前開發(fā)的一個(gè)app?8. javascript - 為什么這個(gè)點(diǎn)擊事件需要點(diǎn)擊兩次才有效果9. javascript - ionic2 input autofocus 電腦成功,iOS手機(jī)鍵盤不彈出10. vue.js - vue 打包后 nginx 服務(wù)端API請(qǐng)求跨域問題無法解決。
