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. javascript - h5微信中怎么禁止横屏2. css - div设置float:left后高度设置自动会无效 ?3. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””4. python - 怎么查看网址做的是什么反爬虫5. 为什么我ping不通我的docker容器呢???6. node.js - webpack required打包问题7. boot2docker无法启动8. docker - 各位电脑上有多少个容器啊?容器一多,自己都搞混了,咋办呢?9. nignx - docker内nginx 80端口被占用10. docker-compose中volumes的问题

网公网安备