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. windows误人子弟啊2. mysql优化 - MySQL如何为配置表建立索引?3. 我在网址中输入localhost/abc.php显示的是not found是为什么呢?4. 关于mysql联合查询一对多的显示结果问题5. 如何用笔记本上的apache做微信开发的服务器6. 实现bing搜索工具urlAPI提交7. 数据库 - Mysql的存储过程真的是个坑!求助下面的存储过程哪里错啦,实在是找不到哪里的问题了。8. python - linux怎么在每天的凌晨2点执行一次这个log.py文件9. MySQL主键冲突时的更新操作和替换操作在功能上有什么差别(如图)10. 冒昧问一下,我这php代码哪里出错了???
