gpt4 book ai didi

java - Spring Aop 错误无法为此建议延迟构建 thisJoinPoint

转载 作者:行者123 更新时间:2023-12-01 13:39:49 27 4
gpt4 key购买 nike

切入点声明:

@Pointcut(value="com.someapp.someservice.someOperation() && args(t,req)",argNames="t,req")
private void logOperationArg(final String t,final String req)
{
}

建议声明未编译:

@Before(value="logOperationArg(t,req)")
public void logBeforeOperationAdvice(JoinPoint jp, final String t, final String req){
...
}

使用 Aspectj-maven-plugin(1.5 版本)编译 Aspect 时,出现错误“无法为该建议延迟构建 thisJoinPoint,因为它没有合适的防护 [Xlint:noGuardForLazyTjp]”

但是相同的建议在没有 JoinPoint 参数的情况下编译。

建议声明编译:

@Before(value="logOperationArg(t,req)")
public void logBeforeOperationAdvice(final String t, final String req){
...
}

最佳答案

Spring AOP 仅支持方法连接点,因为它基于动态代理,它在需要时创建代理对象(例如,如果您正在使用ApplicationContext,它将在从BeanFactory加载bean后创建)

使用execution()语句来匹配方法执行的连接点。

例如:

class LogAspect {

@Before("execution(* com.test.work.Working(..))")
public void beginBefore(JoinPoint join){

System.out.println("This will be displayed before Working() method will be executed");
}

现在如何声明您的 BO:

//.. declare interface

然后:

class BoModel implements SomeBoInterface {

public void Working(){
System.out.println("It will works after aspect");
}
}

execution() 语句是一个 PointCut 表达式,用于告诉应在何处应用您的建议。

如果你喜欢使用@PointCut,你可以这样做:

class LogAspect {

//define a pointcut
@PointCut(
"execution(* com.test.work.SomeInferface.someInterfaceMethod(..))")
public void PointCutLoc() {
}

@Before("PointCutLoc()")
public void getBefore(){
System.out.println("This will be executed before someInterfaceMethod()");
}

}

第二部分:

此外,该错误表明您没有对您的建议加以警惕。从技术上讲,guard 使您的代码更快,因为您不需要每次执行它时都构造 thisJoinPoint。所以,如果它没有意义,你可以尝试忽略它

canNotImplementLazyTjp = ignore
multipleAdviceStoppingLazyTjp=ignore
noGuardForLazyTjp=ignore

关于java - Spring Aop 错误无法为此建议延迟构建 thisJoinPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20930098/

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