【问题标题】:Spring annotations: Is there a similar annotation like @ExceptionHandler used in Controller stereotypes for @Repository or @Service stereotypes?Spring注解:在@Repository 或@Service 原型的控制器原型中是否有类似@ExceptionHandler 的注释?
【发布时间】:2012-02-24 19:48:12
【问题描述】:

出于好奇,我尝试查看是否可以在存储库中使用org.springframework.web.bind.annotation.ExceptionHandler。正如预期的那样,注释被忽略了。

我有一个低优先级的检测服务,我不希望出现异常。与其使用 try/catch 对检测服务上的每个方法进行编码,我更希望为该服务提供一个 @ExceptionHandler 方法 - 类似于 Spring @Controller 中使用的技术。

想法?

【问题讨论】:

    标签: java spring jakarta-ee spring-mvc


    【解决方案1】:

    我认为您现在无法从 Spring 获得此功能(也许您想提出功能请求...),但您应该可以使用 Spring AOP 轻松完成此操作。

    @Aspect
    public class DaoAspect {
        @AfterThrowing(/*any method in a @Repository class that is not annotated with @ExceptionHandler*/ throwing="ex")
        public void doRecoveryActions(DataAccessException ex) {
            //find method of throwing class that can handle the exception via @ExceptionHandler
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 2013-03-09
      • 2013-01-10
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      相关资源
      最近更新 更多