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. android - weex 項(xiàng)目createInstanceReferenceError: Vue is not defined2. android - 哪位大神知道java后臺(tái)的api接口的對(duì)象傳到前端后輸入日期報(bào)錯(cuò),是什么情況?求大神指點(diǎn)3. pdo 寫入到數(shù)據(jù)庫(kù)的內(nèi)容為中文的時(shí)候?qū)懭雭y碼4. PHPExcel表格導(dǎo)入數(shù)據(jù)庫(kù)怎么導(dǎo)入5. javascript - 如圖,百度首頁,查看源代碼為什么什么都沒有?6. vue2.0+webpack 如何使用bootstrap?7. PHP類封裝的插入數(shù)據(jù),總是插入不成功,返回false;8. mac連接阿里云docker集群,已經(jīng)卡了2天了,求問?9. 只允許微信登錄設(shè)置在哪里實(shí)現(xiàn)?CI框架10. java - 微信開發(fā)網(wǎng)頁授權(quán)
