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. node.js - nodejs debug问题2. python - flask post提交timestamp不能作为参数,这是为什么?3. spring-mvc - spring-session-redis HttpSessionListener失效4. 在windows下安装docker Toolbox 启动Docker Quickstart Terminal 失败!5. 百度地图api - Android 百度地图 集成了定位,导航 相互的jar包有冲突?6. node.js - express框架,设置浏览器从缓存中读取静态文件,只有js从缓存中读取了,css还有一些图片为何没有从缓存中读取?7. 如何解决Centos下Docker服务启动无响应,且输入docker命令无响应?8. 做Redis集群的时候,可不可以将Master实例和Slave实例放在一个主机当中?9. javascript - 如何获取未来元素的父元素在页面中所有相同元素中是第几个?10. mysql - 一个sql的问题

网公网安备