gpt4 book ai didi

java - 如何将 spring aop 与来自外部 jar 的类一起使用

转载 作者:行者123 更新时间:2023-12-05 07:46:57 27 4
gpt4 key购买 nike

我需要拦截 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#handle 方法,为此我使用了 spring aop。它是来自 spring-webmvc.jar 的 spring 类。它在请求处理期间用于 org.springframework.web.servlet.DispatcherServlet 到我的其余 mvc 服务。

AOP 配置:

<bean id="contextAspect" class="com.eightbitlab.spring3rest.config.ContextAspect"/>

<aop:aspectj-autoproxy proxy-target-class="true"/>
<aop:config>
<aop:aspect ref="contextAspect">
<aop:pointcut id="contextPointcut" expression="execution (* org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handle(..))"/>
<aop:around method="around" pointcut-ref="contextPointcut"/>
</aop:aspect>
</aop:config>

切面 bean :

@Aspect
public class ContextAspect {
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
Logger.getAnonymousLogger().info("start aspect");
Object proceeded = joinPoint.proceed();
Logger.getAnonymousLogger().info("end aspect");
}
}

如果我将此方面与自己的类一起使用,则执行方面方法。我认为问题是来自外部 jar 的类,但我找不到解决方案...

最佳答案

如果您查看 handle 的签名方法将其标记为 final

由于 Spring AOP 无法通知 final 方法(请参阅以下 Spring 文档的摘录 here ),请考虑改为拦截/通知您的 Controller 。

You cannot add advice to final methods when you use Spring MVC. For example, you cannot add advice to the AbstractController.setSynchronizeOnSession() method. Refer to Section 10.6.1, “Understanding AOP proxies” for more information on AOP proxies and why you cannot add advice to final methods.

P.S.:另外看看here以及进一步了解 AOP 代理。

关于java - 如何将 spring aop 与来自外部 jar 的类一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40370931/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com