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. python - 有什么好的可以收集货币基金的资源?2. css3不包括3. mysql优化 - mysql 分页查询优化。4. css - html表格 td 宽度随着固定为内容宽度5. mysql - 仅仅只是把单引号与反斜杠转义不用prepare statement能否避免sql注入?6. 求救一下,用新版的phpstudy,数据库过段时间会消失是什么情况?7. css3 - 扩展本地项目中阿里的iconfont字体库,添加图标字体8. javascript - 页面加载慢,有什么优化方案9. javascript - 如何将一个长为365的数组对象按月份切割成12组?10. css - flex布局垂直效果实现不了?

网公网安备