java - spring AOP 不生效
問(wèn)題描述
寫(xiě)了個(gè)切面, 如果切點(diǎn)定義聲明在Controller上面的方法,這對(duì)應(yīng)的通知能夠執(zhí)行, 如果不是Controller直接調(diào)用的則通知無(wú)法執(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方法沒(méi)有在controller中顯示調(diào)用, 所有即便執(zhí)行了test方法也不會(huì)通知也沒(méi)有被觸發(fā)執(zhí)行.基于Spring MVC.
問(wèn)題解答
回答1:Spring AOP 只對(duì) Bean 進(jìn)行代理,如果你的實(shí)例不是從 Spring 獲取來(lái)的 Bean 而是自己實(shí)例出來(lái)的它是沒(méi)法進(jìn)行代理的。
相關(guān)文章:
1. MySQL數(shù)據(jù)庫(kù)中文亂碼的原因2. 如何解決Centos下Docker服務(wù)啟動(dòng)無(wú)響應(yīng),且輸入docker命令無(wú)響應(yīng)?3. mysql - 新浪微博中的關(guān)注功能是如何設(shè)計(jì)表結(jié)構(gòu)的?4. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問(wèn)題。5. dockerfile - [docker build image失敗- npm install]6. angular.js - 關(guān)于$apply()7. android-studio - Android Studio 運(yùn)行項(xiàng)目的時(shí)候一堆警告,跑步起來(lái)!?8. 我在centos容器里安裝docker,也就是在容器里安裝容器,報(bào)錯(cuò)了?9. angular.js - Ionic 集成crosswalk后生成的apk在android4.4.2上安裝失敗???10. nignx - docker內(nèi)nginx 80端口被占用
