【问题标题】:Not able to run @Advice catch Exception block无法运行@Advice catch 异常块
【发布时间】:2021-04-14 19:19:24
【问题描述】:

这是我创建的 Aop 特性,用于在我的主应用程序引发错误后运行。

package com.demo.aspects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAroundAspect {

  @Around("execution(* com.demo.aop.*.getFortune(..))")
  public Object aroundAdviceDemo(ProceedingJoinPoint thePJP) throws Throwable
  {
    System.out.println("run this before the fortune method");

    Object result = null;
    try {
      result = thePJP.proceed();
    }
    catch(Exception exc)
    {   //this should run but is not running
      System.out.println("Catching the " + exc.getMessage());
    }


    System.out.println("Run this after the fortune service");

    return result;
  }
}

这是我的 trafficFortuneservuce 类,它有一个引发错误的方法,我正在尝试使用我的 @Around 建议来捕获该错误。

package com.demo.aop;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class TrafficFortuneService {

  public String getFortune(boolean tripwire)
  {
    if(tripwire)
    {    //this is running but it should go to the advice catch block
      throw new RuntimeException("Inside run time exception in the fortune service");
    }

    return "Today is your lucky day";
  }
}

这是我运行应用程序的主要方法类。

package com.demo.aop.app;

import java.util.List;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.demo.aop.Account;
import com.demo.aop.AccountDAO;
import com.demo.aop.AppConfig;
import com.demo.aop.MemberDAO;
import com.demo.aop.TrafficFortuneService;

public class AroundDemoApp {

  public static void main(String[] args) {
    //Getting context object
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
    TrafficFortuneService theService = context.getBean("trafficFortuneService", TrafficFortuneService.class);
    boolean tripwire = true;
    String temp =   theService.getFortune(tripwire);

    System.out.println(temp);

    context.close();
  }
}

这是输出,打印我在trafficfortuneservice 类中声明的消息

Exception in thread "main" java.lang.RuntimeException: Inside run time exception in the fortune service
    at com.demo.aop.TrafficFortuneService.getFortune(TrafficFortuneService.java:15)
    at com.demo.aop.app.AroundDemoApp.main(AroundDemoApp.java:20)

【问题讨论】:

  • 问题不是没有被捕获,而是aroundAdviceDemo 根本没有被调用(因为它应该显示你声明的第一个system.out)
  • 欢迎来到 SO。对那些试图通过回复他们来帮助你的人表示尊重会很好。如果由于某种原因到目前为止发布的答案都没有帮助您回答您的问题,您的反馈将帮助我们最终解决您的问题。如果其中一个解决了您的问题,请鼓励通过单击灰色复选标记将其标记为已接受,使其变为绿色。 ??????

标签: java aop spring-aop


【解决方案1】:

@EnableAspectJAutoProxy 添加到您的spring 配置类(添加到MyAroundAspect.java 即可)。

Spring Boot 会自动开启这个功能,@SpringBootApplicationhere 描述的一样,所以在正常的spring boot 应用中,不需要手动标记这个注解。但在您的情况下,您是手动启动应用程序,所以它不起作用。

【讨论】:

    【解决方案2】:

    不要将您的普通应用程序类设为@Aspect。首先,它没有任何意义。其次,Spring AOP 方面不能相互提供建议,因此您实际上是在阻止自己实现目标,这是在自取其辱。


    更新:我说的是这个:

    //@Aspect <-- You got to get rid of this, the serice is not an aspect!
    @Component
    public class TrafficFortuneService {
      // (...)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 2010-12-06
      相关资源
      最近更新 更多