@ControllerAdvice



Any class that is annotated with @ControllerAdvice is called a controller advice.
The @ControllerAdvice annotation is already have @Component annotation applied. 

So, it will be pickedup automatically by the component scanning.
Controller advice class can have methods annotated with following annotation.

@ExceptionHandler
@InitBinder
@ModelAttribute


Methods which are annotated with above annotation are applied global to all the methods which are annotated with the @RequestMapping annotation in all the controller classes.
The best use of the @ControllerAdvice is to handle all the exceptions in a single class.


Example:
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class ControllerExceptionHandler{
    @ExceptionHandler(SQLException.class)
    public String handleSqlException(HttpServletRequest httpRequest, Exception ex){

        LOG.info("handleSqlException() [Start]")

        LOG.debug("Called URL: {}", httpRequest.getRequestURL());
        LOG.debug("Error: {}, ",ex.getMessage());


        LOG.info("
handleSqlException() [End]");
        return "dberror";
    }
}


So, here whenever SQLException is thrown by any controller class then handleSqlException() method will be called by the spring framework
and flow will execute further.