gpt4 book ai didi

java - AspectJ 拦截器不工作

转载 作者:行者123 更新时间:2023-12-02 13:40:24 25 4
gpt4 key购买 nike

我创建了一个 AspectJ 拦截器,例如

@Aspect
public class RequestSpecificServiceAspect {
@Pointcut("execution( * com.mycompany.c.d.doesTreatmentEqualsAndTrigger(..))")
private void callInterceptor(){}

@Before("callInterceptor()")
public void getCallStack(){
StackTraceElement[] callingStack = Thread.currentThread().getStackTrace();
PopulateServiceDependentMap populateServiceDependentMap = new PopulateServiceDependentMap();
populateServiceDependentMap.populateMap(callingStack, "ServiceName");
}
}

这工作得很好,因为这是一个试用代码,我现在将其替换为我想要的实际拦截器,如下所示

@Pointcut("execution( * mycompany.f.g.findPluginForRequest(..)) && args(request)")
private void actualInterceptor(BSFBatchRequest request){}

@Before("actualInterceptor(request)")
public void getBSFCall(BSFBatchRequest request){
StackTraceElement[] callingStack = Thread.currentThread().getStackTrace();
PopulateServiceDependentMap populateServiceDependentMap = new PopulateServiceDependentMap();
populateServiceDependentMap.populateMap(callingStack, request);
}

但是现在我的拦截器没有拦截对 findPluginForRequest() 的调用功能。为什么会发生这种情况以及如何解决它?

这是我的 spring 配置文件(.xml):

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="no">
<context:annotation-config/>

<aop:aspectj-autoproxy/>

<bean name="com.dpx.dependencyGraphFactory" class="com.mycompany.dpx.dependencytree.ModuleToModuleDependencyGraphFactory"></bean>
<bean name="com.dpx.serviceInterceptor" class="com.mycompany.dpx.dependencytree.RequestSpecificServiceAspect"/>

</beans>

findPluginForRequest() 的签名是 private AllPurposeCache<BSFBatchRequest, BSFReply> findPluginForRequest(final BSFBatchRequest request) 。我尝试将切入点更改为

@Pointcut("execution(private * mycompany.f.g.findPluginForRequest(..)) && args(request)")
private void actualInterceptor(BSFBatchRequest request){}

但是还是不行。

最佳答案

现在您终于共享了方法签名,我可以回答您的问题:

private AllPurposeCache<BSFBatchRequest, BSFReply> findPluginForRequest(
final BSFBatchRequest request
)

Spring AOP 不如 AspectJ 强大,因为它不直接编织字节码,而是基于通过 JDK 或 CGLIB 创建/使用动态代理。动态代理只是实现接口(interface)的子类或类。因此它们只重写公共(public)方法。你的方法是私有(private)的,因此Spring AOP无法拦截它。这记录在 Spring AOP manual 中:

Due to the proxy-based nature of Spring’s AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn’t applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only! If your interception needs include protected/private methods or even constructors, consider the use of Spring-driven native AspectJ weaving instead of Spring’s proxy-based AOP framework. This constitutes a different mode of AOP usage with different characteristics, so be sure to make yourself familiar with weaving first before making a decision.

为了让它工作,要么公开该方法,要么切换到 AspectJ。

P.S.:这本来可以更容易、更快。请学习how to ask a question on SO以及如何提供 minimal, complete, and verifiable example 。谢谢。

关于java - AspectJ 拦截器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42762484/

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