本文介紹了Spring AOP錯(cuò)誤無(wú)法懶惰地為此建議構(gòu)建thisJoinPoint的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
切入點(diǎn)聲明:
@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版)編譯方面時(shí),出現(xiàn)錯(cuò)誤"can not build thisJoinPoint lazily for this advice since it has no suitable guard [Xlint:noGuardForLazyTjp]"
但編譯相同的建議時(shí)不使用JoinPoint參數(shù)。
建議聲明匯編:
@Before(value="logOperationArg(t,req)")
public void logBeforeOperationAdvice(final String t, final String req){
...
}
推薦答案
Spring AOP
僅支持method join points
,因?yàn)樗?code>dynamic proxies在需要時(shí)創(chuàng)建代理對(duì)象(例如,如果您使用的是ApplicationContext,則它將在從BeanFactory
加載Bean后創(chuàng)建)
使用execution()
語(yǔ)句匹配作為方法執(zhí)行的連接點(diǎn)。
例如:
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");
}
現(xiàn)在如何聲明您的BO:
//.. declare interface
然后:
class BoModel implements SomeBoInterface {
public void Working(){
System.out.println("It will works after aspect");
}
}
execution()
語(yǔ)句是PointCut表達(dá)式,用于告知應(yīng)將建議應(yīng)用于何處。
如果您喜歡使用@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部分:
此外,該錯(cuò)誤還表明您沒(méi)有保護(hù)您的建議。從技術(shù)上講,Guard使您的代碼更快,因?yàn)槟恍枰诿看螆?zhí)行它時(shí)都構(gòu)造thisJoinPoint。因此,如果沒(méi)有意義,您可以嘗試忽略它
canNotImplementLazyTjp = ignore
multipleAdviceStoppingLazyTjp=ignore
noGuardForLazyTjp=ignore
這篇關(guān)于Spring AOP錯(cuò)誤無(wú)法懶惰地為此建議構(gòu)建thisJoinPoint的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,