Managing exceptions
Spring 5.0 has built-in API classes such as HandlerExceptionResolver
and @AdviceController
to handle @Controller
exceptions, but this recipe will create another way to manage exceptions through an improvised handler implemented using the AOP paradigm.
Getting started
Open ch05
and add another aspect component that will monitor all methods of EmployeeServiceImpl
and will catch all types of exceptions once encountered.
How to do it...
Let us improvise exception handling using AOP concepts by doing these steps:
- Just like in the previous recipe, verify if
Employee
models classes and its service implementations are in their respective packages. We will still be using theJdbctemplate
-based CRUD transactions. - Other than transactions,
@Aspect
can also be used to trace and log some exceptions. Let us now add an aspect namedExceptionUpdateAspect
in the packageorg.packt.aop.transaction.core
that will contain two@AfterThrowing
advices, namelylogExceptionUpdateEmp()
, which will be...