Spring Aop Error无法为此建议懒惰地构建thisJoinPoint

时间:2014-01-05 04:35:03

标签: java spring spring-aop aspectj-maven-plugin

切入点声明:

@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时,出现错误"can not build thisJoinPoint lazily for this advice since it has no suitable guard [Xlint:noGuardForLazyTjp]"

但是没有JoinPoint参数,同样的建议会编译。

建议声明编制:

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

1 个答案:

答案 0 :(得分:1)

Spring AOP仅支持method join points,因为它基于dynamic proxies,如果需要,会创建代理对象(例如,如果您使用的是ApplicationContext,则会在加载bean后创建它)来自BeanFactory

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

}

2部分:

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

canNotImplementLazyTjp = ignore
multipleAdviceStoppingLazyTjp=ignore
noGuardForLazyTjp=ignore