AfterThrow基于异常类型的建议

时间:2014-01-16 12:27:09

标签: java spring spring-aop

我正在尝试根据方法抛出的异常类型创建多个AfterThrowing建议机制。它没有被调用。以下是代码段:

@Component
@Configuration
@EnableAspectJAutoProxy
@Aspect
public class ExceptionAspect {

    @Pointcut("within( sampleapp.config.*)")
    public void allInitializationMethods() {
    }

    @AfterThrowing(pointcut="allInitializationMethods()",throwing="ex" )
    public void allInitializationMethodsBefore(FatalInitializationException ex) {
        System.out.println("Point cut executed");   
    }

    @AfterThrowing(pointcut="allInitializationMethods()",throwing="ex" )
    public void allInitializationMethodsBefore(RetryToRecoverException ex) {
        System.out.println("Point cut executed");   
    }
}

1 个答案:

答案 0 :(得分:0)

这对我很有效。我认为配置中唯一缺少的部分是@ComponentScan。但我相信这将是你的配置类因此没有太多关于它。 如果你还没有添加@ComponentScan,那根本不会加载你的方面。

这是我的AppConfig类。

@Configuration
@ComponentScan("com.stackoverflow.aopquestion")
@EnableAspectJAutoProxy
public class AppConfig {

}

我的主要课程

public class ApplicationMain {

   public static void main(final String[] args) {
      final AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(AppConfig.class);
      // appContext.scan("com.stackoverflow.aopquestion");
      // appContext.refresh();
      final FatalAppExceptionGenerator exGen = (FatalAppExceptionGenerator) appContext
            .getBean("fatalappExceptionGenerator");
      try {
         exGen.throwingFatalApp();
      }
      catch (final FatalApplicationError e) {
         // TODO Auto-generated catch block
         // e.printStackTrace();
      }

   }

}

最后我的方面

@Component
@Aspect
public class AnAspect {

   @Pointcut(value = "within(com.stackoverflow.aopquestion.*)")
   public void allExceptionPointcuts() {

   }

   @AfterThrowing(value = "allExceptionPointcuts()", throwing = "ex")
   public void doCheck(final FatalApplicationError ex) {
      System.out.println("Invoked the afterthrowing aspect");
      // ex.printStackTrace();
   }

   @Before(value = "within(com.stackoverflow.aopquestion.FatalAppExceptionGenerator+) && execution(* throwingFatalApp(..))")
   public void doValidate(final JoinPoint jp) {
      System.out.println("Invoking before advice");
   }

}

这是输出。

Jan 20, 2014 1:31:32 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3a6af2fb: startup date [Mon Jan 20 13:31:32 IST 2014]; root of context hierarchy Jan 20, 2014 1:31:32 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@418f12dc: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,appConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,anAspect,fatalappExceptionGenerator,org.springframework.aop.config.internalAutoProxyCreator]; root of factory hierarchy 
  

在建议之前调用调用了后续方面