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. docker api 開發的端口怎么獲取?2. docker gitlab 如何git clone?3. debian - docker依賴的aufs-tools源碼哪里可以找到啊?4. macos - mac下docker如何設置代理5. angular.js使用$resource服務把數據存入mongodb的問題。6. docker網絡端口映射,沒有方便點的操作方法么?7. docker 下面創建的IMAGE 他們的 ID 一樣?這個是怎么回事????8. docker - 如何修改運行中容器的配置9. dockerfile - 我用docker build的時候出現下邊問題 麻煩幫我看一下10. docker start -a dockername 老是卡住,什么情況?

網公網安備