【问题标题】:Exception using @After(@annotation) pointcut with AspectJ + SpringBoot使用带有 AspectJ + SpringBoot 的 @After(@annotation) 切入点的异常
【发布时间】:2019-12-07 23:39:11
【问题描述】:

各位程序员大家好。

我正在开发一个 SpringBoot Rest API,我正在尝试使用一个 Aspect,它在执行带有自定义注释的方法后执行一些操作。

所以我有这个自定义注释:

@Retention(RUNTIME)
@Target(METHOD)
public @interface PublishMQ {
    String destinyName() default "";
    boolean skipNull() default true;
}

我有这个方面:

@Aspect
@Component
public class PublishMQAspect {

// ...
    @After("@annotation(br.com.powertiss.utils.transaction.PublishMQ)")
    public Object publishChangeToMQ(Object returnValue, PublishMQ publishMQ) throws Throwable {
// ...

我正在尝试在服务中使用它们:

@Service
public class OperatorService {
// ...
    @PublishMQ(destinyName = "queues/Opera")
    public Operator salve(Operadora operator) {
// ...

但我在启动时遇到了给定的异常:

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

PublishMQ 和 PublishMQAspect 与 OperatorService 位于单独的 jar 中,但我认为这应该不是问题。

我花了几个小时尝试了很多东西,但不明白为什么 AspectJ 会引发这个异常。你们能帮忙吗?谢谢。

【问题讨论】:

  • 我猜这是因为 AspectJ 不知道切入点方法的第一个参数是什么。您需要以某种方式指示它是方法的返回值。
  • 确保你导入的类是org.aspectj.lang.JoinPoint
  • 另外,不应该是@AfterReturning,因为你想要返回值?

标签: java spring-boot aop aspectj spring-aop


【解决方案1】:

在我回答之前,让我建设性地批评你提出问题的方式:问题不清楚。为什么提供不连贯的代码 sn-ps 而不是完整的类或方法?具体来说,您没有在方面建议中显示您实际想要使用的方法参数。所以现在我必须猜测(我不喜欢这个,因为现在我的工作量是必要的 4 倍,见下文)。我建议您了解 MCVE 是什么 - 4k 信誉用户应该已经知道 - 并尝试在未来提出更好的问题。


除了始终隐式可用的JoinPoint 参数外,您不能使用不会出现在切入点或结果中的建议方法参数。您应该阅读一些 AspectJ 文档以了解有关切入点和建议语法的更多信息。

您有一个选择:在您的建议方法中,您不需要返回值和注释...

@After("@annotation(br.com.powertiss.utils.transaction.PublishMQ)")
public Object publishChangeToMQ(JoinPoint thisJoinPoint) throws Throwable {

...或者您只需要注释值...

@After("@annotation(publishMQ)")
public Object publishChangeToMQ(JoinPoint thisJoinPoint, PublishMQ publishMQ) throws Throwable {

...或者您只需要返回值...

@AfterReturning(pointcut = "@annotation(br.com.powertiss.utils.transaction.PublishMQ)", returning = "returnValue")
public Object publishChangeToMQ(JoinPoint thisJoinPoint, Object returnValue) throws Throwable {

...或者你需要返回值和注解...

@AfterReturning(pointcut = "@annotation(publishMQ)", returning = "returnValue")
public Object publishChangeToMQ(JoinPoint thisJoinPoint, PublishMQ publishMQ, Object returnValue) throws Throwable {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-05
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多