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. mysql日期类型默认值’0000-00-00’ 报错2. 求救一下,用新版的phpstudy,数据库过段时间会消失是什么情况?3. mysql replace 死锁4. mysql - C#连接数据库时一直这一句出问题int i = cmd.ExecuteNonQuery();5. MYSQL 根据两个字段值查询 但两个值的位置可能是互换的,这个怎么查?6. extra没有加载出来7. android - 安卓做前端,PHP做后台服务器 有什么需要注意的?8. javascript - 微信网页开发从菜单进入页面后,按返回键没有关闭浏览器而是刷新当前页面,求解决?9. php传对应的id值为什么传不了啊有木有大神会的看我下方截图10. mysql - ubuntu开启3306端口失败,有什么办法可以解决?
