【问题标题】:AspectJ expression gives formal unbound in pointcut errorAspectJ 表达式在切入点错误中给出正式的未绑定
【发布时间】:2012-09-04 06:50:15
【问题描述】:

我在 aspectJ 中有这样的表达:

@Pointcut("within(com.param.cpms.dao.impl.ProjectMetaDaoImpl)")
public void daoExceptionHandle() {

}

Spring 3.0 启动时,我收到以下错误

nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut

【问题讨论】:

    标签: java spring spring-aop pointcut


    【解决方案1】:

    可能问题不在于您的切入点,而在于使用该切入点和使用切入点中不存在的参数的建议。只需从建议中删除参数(好吧,或者将其添加到切入点)。

    【讨论】:

    • 如何添加参数?
    • 说真的:如果不提供任何上下文,你不能问这个问题。你想用建议参数做什么?您想将什么绑定到未绑定的变量?根据答案,您将使用this()target()args()@annotation() 或其他内容。 P.S.:我不是一个灵媒——也许是一个心灵学家,但那是一个不同的话题。 ;-)
    【解决方案2】:

    它是 Joinpoint ("p 小写)

    org.aopalliance.intercept.Joinpoint;
    

    改为JointPoint("P大写)

    org.aspectj.lang.JoinPoint; 
    

    【讨论】:

    • 这是我的问题。谢谢!
    【解决方案3】:

    这篇文章相当陈旧,但为了完整起见,如果您使用 @Around 建议,我会添加另一个原因。

    根据Spring AspectJ documentation,通知的第一个参数必须是ProceedingJoinPoint。如果它丢失,您将收到此异常消息。遗憾的是,该异常并没有指出错误的建议,因此解决该错误是一个偶然的机会。

    【讨论】:

    • 感谢分享!这正是我的问题。我实现了MethodInterceptor 并将@Around 注释放在它的invoke 方法上。但现在我知道,@Around 不是为MethodInterceptor.invoke 制作的。
    【解决方案4】:

    由于类的错误导入,我得到了这个错误。我应该导入 import org.aspectj.lang.JoinPoint class ,而是从不同的包中导入了一些其他的 Joinpoint 类。

    【讨论】:

      【解决方案5】:

      我也遇到了这个问题,在我的情况下,这是一个错误的导入:org.aopalliance.intercept.Joinpoint;

      必须是:org.aspectj.lang.JoinPoint;

      【讨论】:

        【解决方案6】:

        有时原因可能是这个。

         public void afterReturning(JoinPoint joinPoint, Object result)
        

        只需删除Object result,如下所示,它对我有用。

        public void afterReturning(JoinPoint joinPoint)
        

        【讨论】:

          【解决方案7】:

          我遇到了同样的错误,在我的场景中我使用了两个方法参数

          public void methodName(JoinPoint joinPoint ,HttpServletRequest request) throws
          

          我的注释就像

          @Before("execution(public * com.java.controller.*Controller.*(..))")
          

          作为我添加的解决方案

          args(请求,..)

          @Before("execution(public * com.java.controller.*Controller.*(..)) && args(request,..)")
          

          【讨论】:

            【解决方案8】:

            如果您使用的是基于 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>
            

            然后在两种情况下,您会收到错误消息:

            1. 如果在切入点表达式中,方法,即我们的例子中的 display() 定义了没有任何参数,而在实际的类方法中有一些参数。
            2. 如果在 before 通知中,即 aop:before,method="before" 没有定义 arg-names 并且在实际的通知类中,方法 "before" 有一些参数。

            最终当XML中定义的方法参数与实际方法不匹配时,就会出现这个错误。

            【讨论】:

              【解决方案9】:

              这不是你的答案,但可能会对你有所帮助。

              Spring AOP Tutorial你可以参考这个教程

              @Before("execution(* com.de.controller..*(..))")
              public void beforeLoggerAdvice(JoinPoint joinPoint, WebRequest request) {
                  DeUtil.looger.info("--working");
              }
              

              我得到了同样的异常,但由于 WebRequest,我删除了它并使用了替代方法

              HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
              

              【讨论】:

                【解决方案10】:

                切入点中的正式未绑定异常也发生在 AOP 中的两个响应中。

                原因一:如果返回通知后没有return语句

                对于基于 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
                

                原因2:如果没有投掷在投掷后提示

                对于基于 XML 的实现

                <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
                

                【讨论】:

                  猜你喜欢
                  • 2015-10-25
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2022-01-06
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多