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. html - 爬虫时出现“DNS lookup failed”,打开网页却没问题,这是什么情况?2. javascript - echart+百度地图3. web - Rails3使用form_for时出现undefined method `*_path’错误。4. css - autoprefixer没有添加web-kit前缀5. 无效的配置对象已使用与API模式不匹配的配置对象初始化了Webpack6. javascript 开发百度地图7. nginx - 关于vue项目部署到ngnix后出现的问题8. angular.js - 百度爬虫如何处理“#”符号?9. nosql - mongodb 多组数据不固定字段查询问题 [百度党请绕道]10. css3 - 请问,如何通过CSS实现高度height随宽度width变化而变化,保持长宽比例不变,宽度是根据父元素宽度变化的?
