Java AspectJ表达式在切入点错误中给出了形式上的未绑定
我在aspectJ中有一个表达式:Java AspectJ表达式在切入点错误中给出了形式上的未绑定,java,spring,spring-aop,pointcut,Java,Spring,Spring Aop,Pointcut,我在aspectJ中有一个表达式: @Pointcut("within(com.param.cpms.dao.impl.ProjectMetaDaoImpl)") public void daoExceptionHandle() { } 在Spring3.0启动时,我得到以下错误:: nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 问题可能不在切入
@Pointcut("within(com.param.cpms.dao.impl.ProjectMetaDaoImpl)")
public void daoExceptionHandle() {
}
在Spring3.0启动时,我得到以下错误::
nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
问题可能不在切入点,而是在使用该切入点并使用切入点中不存在的参数的建议中。只需从通知中删除该参数(好吧,或者将其添加到切入点)。这篇文章很旧,但为了完整性,如果您使用@Around advice,我将添加另一个原因
根据建议,第一个参数必须是ProceedingJoinPoint。如果它丢失了,您将得到这个异常消息。遗憾的是,异常并没有指向错误中的建议,因此解决该错误是一个碰运气的问题。由于类的错误导入,我得到了这个错误。我应该导入import org.aspectj.lang.JoinPoint类,但是从另一个包导入了其他一些JoinPoint类 我也有这个问题,在我的例子中,这是一个错误的导入源:
org.aopalliance.intercept.Joinpoint代码>
它需要是:org.aspectj.lang.JoinPoint代码>如果您使用的是基于XML的配置,并且您的配置如下所示:
<aop:config>
<aop:aspect ref="bAdvice">
<aop:pointcut id="displayPointcut" expression="execution(* com.example.demo.BusinessClass.display())"/>
<aop:before method="before" pointcut-ref="displayPointcut" />
</aop:aspect>
</aop:config>
然后,在两种情况下,您会得到错误:
如果在切入点表达式中,我们的示例中的方法,即display()定义时没有任何参数,而在实际的类方法中有一些参数
如果在before advice中,即aop:before,method=“before”定义了没有参数名称的参数,并且在实际的advice类中,方法“before”有一些参数
最终,当XML中定义的方法参数与实际方法不匹配时,就会出现此错误。这不是您的答案,但可能会对您有所帮助。
您可以参考本教程
@Before("execution(* com.de.controller..*(..))")
public void beforeLoggerAdvice(JoinPoint joinPoint, WebRequest request) {
DeUtil.looger.info("--working");
}
我得到了相同的异常,但由于WebRequest,我删除了该异常并使用了替代方法
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
它是Joinpoint(“p小写)
更改为JointPoint(“p大写)
有时原因可能是这样的
public void afterReturning(JoinPoint joinPoint, Object result)
只需删除对象结果
,如下所示,它对我有效
public void afterReturning(JoinPoint joinPoint)
AOP中的两个原因也会出现切入点中的正式未绑定异常
原因1:如果在返回通知后的中没有返回语句
用于基于XML的实现
<aop:aspect id="myaspect" ref="trackAspect">
<aop:pointcut id="pointCutAfterReturning" expression="execution(* com.springlearn.Operation.*(..))" />
<aop:after-returning method="myAdvice" returning="result" pointcut-ref="pointCutAfterReturning"/> //Make sure returning result is added
</aop:aspect>
@AfterReturning(
pointcut = "execution(* Operation.*(..))",
returning= "result") //Make sure returning result is added
<aop:aspect id="myaspect" ref="trackAspect" >
<!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing" expression="execution(* com.javatpoint.Operation.*(..))" />
<aop:after-throwing method="myadvice" throwing="error" pointcut-ref="pointCutAfterThrowing" /> //Make sure throwing error is added
</aop:aspect>
@AfterThrowing(
pointcut = "execution(* Operation.*(..))",
throwing= "error") //Make sure throwing error is added
原因2:如果在抛出建议后没有抛出
用于基于XML的实现
<aop:aspect id="myaspect" ref="trackAspect">
<aop:pointcut id="pointCutAfterReturning" expression="execution(* com.springlearn.Operation.*(..))" />
<aop:after-returning method="myAdvice" returning="result" pointcut-ref="pointCutAfterReturning"/> //Make sure returning result is added
</aop:aspect>
@AfterReturning(
pointcut = "execution(* Operation.*(..))",
returning= "result") //Make sure returning result is added
<aop:aspect id="myaspect" ref="trackAspect" >
<!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing" expression="execution(* com.javatpoint.Operation.*(..))" />
<aop:after-throwing method="myadvice" throwing="error" pointcut-ref="pointCutAfterThrowing" /> //Make sure throwing error is added
</aop:aspect>
@AfterThrowing(
pointcut = "execution(* Operation.*(..))",
throwing= "error") //Make sure throwing error is added
我得到了同样的错误,在我的场景中,我使用了两个方法参数
public void methodName(JoinPoint joinPoint ,HttpServletRequest request) throws
我的注释就像
@Before("execution(public * com.java.controller.*Controller.*(..))")
作为解决方案,我添加了
args(请求,…)
如何添加参数?严肃地说:如果不提供任何上下文,就不能问这个问题。您想对advice参数做什么?要绑定到未绑定变量的内容是什么?。根据答案,您将使用this()
,target()
,args()
,@annotation()
或其他方法。旁白:我不是一个媒介——也许是一个心理主义者,但那是一个不同的话题谢谢分享!这正是我的问题所在。我实现了MethodInterceptor
,并在其invoke
方法上添加了@Around
注释。但现在我知道,@Around
并不是为MethodInterceptor.invoke
设计的。这是我的问题。谢谢!