@controlleradvice giving error : scope 'session' is not active for current thread

@controlleradvice giving error : scope 'session' is not active for current thread

我遇到了 Spring mvc 注释 @Controlleradvice 的问题。 我有 2 个控制器 类:UserGapsControllerRegistrationBaseController 类 都使用

  1. @控制器
  2. @Controlleradvice
  3. @Autowired 会话对象
  4. @Scope(WebApplicationContext.SCOPE_SESSION)
当在方法级别使用 @Modelattribute 时,必须使用

@Controlleradvice 注释。所以我在 类 中都有一个用@Modelattribute 注释的方法。 现在的问题是,当我在 UserGapsController.java 中使用 @Controlleradvice 时,应用程序运行良好,当我也在 RegistrationBaseController.java 中使用 @Controlleradvice 时,它​​在运行时出现以下错误:

创建名称为'org.springframework.web.servlet.mvc.method.annotation.requestmappinghandler的bean时出错:调用init方法failed:nested异常是org.springframework.beans.factory.BeanCreationException:创建名称为[的bean时出错=40=]:作用域'session'对于当前线程

不活跃

这个错误的原因是什么,我们不能有2个@Controlleradvice注解类吗?当我在 RegistrationBaseController.java 中评论 @Controlleradvice 时,它​​执行得很好。

您试图让多个 @ControllerAdvice 类 处理不同的异常。

您可以像这样使用 Order over controllerAdvice

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class RegistrationExceptionHandler {

    //...

}

@ControllerAdvice
@Order(Ordered.LOWEST_PRECEDENCE) // or any int value
public class UserGapsExceptionHandler {

    //...

}