title: Is there a way to integrate Springs @ExceptionHandler with Joinfaces
body: <p>i wanted to ask if there is a way to enable Springs <code>@ExceptionHandler</code> capabilities with Joinfaces/Primefaces.</p>
  <p>For now i'm able to handle global <code>@ControllerAdvice</code> beans, but not if the <code>@ExceptionHandler</code> is inside the <code>@Controller</code> class.</p>
  <p>Are there any suggestions on how to solve this topic?</p>
  <p>Here is the code i wrote so far</p>
  <pre><code>@Slf4j
  public class SpringJsfExceptionHandler extends ExceptionHandlerWrapper {
  
      public SpringJsfExceptionHandler(ExceptionHandler wrapped) {
          super(wrapped);
      }
  
      @Override
      public void handle() throws FacesException {
          final Iterator&lt;ExceptionQueuedEvent&gt; queue = getUnhandledExceptionQueuedEvents().iterator();
  
          while (queue.hasNext()) {
              ExceptionQueuedEvent item = queue.next();
              ExceptionQueuedEventContext exceptionQueuedEventContext = (ExceptionQueuedEventContext) item.getSource();
  
              try {
                  Throwable throwable = exceptionQueuedEventContext.getException();
  
                  FacesContext context = FacesContext.getCurrentInstance();
  
                  handleException(context, (Exception) throwable);
  
              } finally {
                  queue.remove();
              }
          }
      }
  
      private void handleException(FacesContext context, Exception throwable) {
          WebApplicationContext applicationContext = resolveApplicationContext(context);
  
          Collection&lt;HandlerExceptionResolver&gt; exceptionResolvers = listExceptionHandlerResolvers(applicationContext);
  
          for (HandlerExceptionResolver resolver : exceptionResolvers) {
              resolver.resolveException(request(context), response(context), null, throwable);
          }
      }
  
      private Collection&lt;HandlerExceptionResolver&gt; listExceptionHandlerResolvers(WebApplicationContext context) {
          return context.getBeansOfType(HandlerExceptionResolver.class).values();
      }
  
      private HttpServletRequest request(FacesContext context) {
          return (HttpServletRequest) context.getExternalContext().getRequest();
      }
  
      private HttpServletResponse response(FacesContext context) {
          return (HttpServletResponse) context.getExternalContext().getResponse();
      }
  
      private WebApplicationContext resolveApplicationContext(FacesContext context) {
          HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
          return WebApplicationContextUtils.findWebApplicationContext(request.getServletContext());
      }
  
  }
  </code></pre>
  <pre><code>public class SpringJsfExceptionHandlerFactory extends ExceptionHandlerFactory {
  
      public SpringJsfExceptionHandlerFactory() {
      }
  
      public SpringJsfExceptionHandlerFactory(ExceptionHandlerFactory wrapped) {
          super(wrapped);
      }
  
      @Override
      public ExceptionHandler getExceptionHandler() {
          return new SpringJsfExceptionHandler(getWrapped() != null ? getWrapped().getExceptionHandler() : null);
      }
  }
  </code></pre>
  <p>This works:</p>
  <pre><code>@ControllerAdvice
  public class GlobalExceptionHandler {
      @ExceptionHandler
      public void handleCalculationException(CalculationException e) {
          FacesContext.getCurrentInstance().
                  addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getMessage()));
      }
  }
  </code></pre>
  <p>This does not work:</p>
  <pre><code>@Data
  @Controller
  @ViewScoped
  public class CalculatorController implements Serializable {
  
      @ExceptionHandler
      public void handleCalculationException(CalculationException e) {
          FacesContext.getCurrentInstance().
                  addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getMessage()));
      }
  [...]
  </code></pre>
  <p>Thanks in advance</p>