不应该建议自我调用,但是应该这样做

编程入门 行业动态 更新时间:2024-10-03 04:38:44
本文介绍了不应该建议自我调用,但是应该这样做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Spring AOP AspectJ出现了一种奇怪的行为:不应建议自我调用,但在我的应用程序中会建议这样做.来自春季文档:

I'm getting a weird behavior by Spring AOP AspectJ: self-invocation shouldn't be adviced, but in my application it does. From Spring documentation:

但是,一旦调用最终到达目标对象, 在这种情况下,SimplePojo引用可能会引起任何方法调用 本身,例如this.bar()或this.foo(),将被调用 反对此参考,而不是代理.这很重要 含义. 这意味着自调用不会导致 与方法调用相关的建议有机会 执行.

However, once the call has finally reached the target object, the SimplePojo reference in this case, any method calls that it may make on itself, such as this.bar() or this.foo(), are going to be invoked against the this reference, and not the proxy. This has important implications. It means that self-invocation is not going to result in the advice associated with a method invocation getting a chance to execute.

但是在我的简单应用程序中,由以下人员组成:

But in my simple application, composed by:

TestAspect

a TestAspect

@Aspect @Component public class TestAspect { private static final Logger logger = LoggerFactory.getLogger(TestAspect.class); @Pointcut("execution(* org.mypackage.TestService.method(..))") public void participateAroundPointcut(){} @Around("participateAroundPointcut()") public void testAround(ProceedingJoinPoint joinPoint) throws Throwable{ logger.debug("Pre-execution;"); joinPoint.proceed(); logger.debug("Post-execution"); } }

TestService:

a TestService:

@Service public class TestService { private static final Logger logger = LoggerFactory.getLogger(TestService.class); public void method(){ logger.debug("Executing method();"); } public void service(){ logger.debug("Executing service();"); this.method(); } }

和一个配置文件:

<context:component-scan base-package="org.mypackage" /> <aop:aspectj-autoproxy proxy-target-class="true" /> <bean id="testAspect" class="org.mypackage.aop.aspects.TestAspect" factory-method="aspectOf"/>

我得到了自调用建议:

DEBUG: org.mypackage.TestService - Executing service(); DEBUG: org.mypackage.aop.aspects.TestAspect - Pre-execution; DEBUG: org.mypackage.TestService - Executing method(); DEBUG: org.mypackage.aop.aspects.TestAspect - Post-execution

我不知道为什么会这样.

I can't figure out why this happens.

推荐答案

您的配置使用AspectJ实现.

它配置有: <aop:aspectj-autoproxy />. (官方文档)

proxy-target-class="true"仅表示将使用CGLIB代理代替Java动态代理.

proxy-target-class="true" only says that CGLIB proxies will be used instead of Java Dynamic Proxies.

我猜您想使用CGLIB代理配置Spring AOP.在这种情况下,配置字符串如下所示:

I guess you want to configure Spring AOP with CGLIB proxies. In that case the config string looks like:

<aop:config proxy-target-class="true"> ... </aop:config>

更多推荐

不应该建议自我调用,但是应该这样做

本文发布于:2023-11-26 09:49:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633625.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这样做   自我   建议

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!