Spring Aop Error Can not build thisJoinPoint lazily for this advice

≡放荡痞女 提交于 2019-12-11 15:14:11

问题


Pointcut declaration:

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

Advice Declaration not compiling:

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

When compiling Aspect with Aspectj-maven-plugin (1.5 version), have error "can not build thisJoinPoint lazily for this advice since it has no suitable guard [Xlint:noGuardForLazyTjp]"

But the same advice compiles without JoinPoint argument.

Advice Declaration compiling:

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

回答1:


Spring AOP only supports method join points because it is based on dynamic proxies which creates proxied object if it is needed (for example if you are using ApplicationContext, it will be created after beans are loaded from BeanFactory)

Use execution() statement to match join points which are methods execution.

For example:

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");
}

And now how to declare your BO:

//.. declare interface

then:

class BoModel implements SomeBoInterface {

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

execution() statement is a PointCut expression to tell where your advice should be applied.

If you like to use @PointCut, you can do something like this:

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()");
      }

}

Part2:

Also,the Error shows that you haven't put a guard on your advice. Technically guard makes your code faster, because you do not need construct thisJoinPoint everytime you execute it. So, if it does not make sense you can try to ignore it

canNotImplementLazyTjp = ignore
multipleAdviceStoppingLazyTjp=ignore
noGuardForLazyTjp=ignore


来源:https://stackoverflow.com/questions/20930098/spring-aop-error-can-not-build-thisjoinpoint-lazily-for-this-advice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!