Adding Around advice

As we've already discussed, around advice is invoked both before and after the business logic method only if the execution is successful. Let's add around advice in the application by performing these steps:

  1. Add an aroundAdvise() method in MyLoggingAspect. This method must have one of its argument as ProceedingJoinPoint to facilitate the flow of the application to the join point. The code will be as follows:

The part before proceed() will get invoked before the Bussiness Logic method, which we refer to as Preprocessing. The ProceedJoinPoint'sproceed() method takes the flow to the respective join point. If the joinpoint executes successfully, the part after proceed will be executed, which we refer to as Post processing. Here, we find the time taken to complete the process by taking the difference in time at preprocessing and post-processing.

The Joinpoint where we want to weave the aspect returns int, so, the aroundAdvise() method also returns a value of the same type. If, in case, we add void instead of int, we will get the following exception:

Exception in thread "main"org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int com.packt.ch03.dao.BookDAO.addBook(com.packt.ch03.beans.Book) 
  1. Let's now add around advice in myLogger, as shown in the following code line:
<aop:around pointcut-ref="pointcut1"method="aroundAdvise"/> 
  1. Execute MainBookDAO to the following log on the console while commenting the advice configured previously:
1016 [main] INFO  com.packt.ch04.aspects.MyLoggingAspect  - around advise before int com.packt.ch03.dao.BookDAO.addBook(Book) B.L.method getting invoked 
1402 [main] INFO  com.packt.ch04.aspects.MyLoggingAspect  - number of rows affected:-1 
1402 [main] INFO  com.packt.ch04.aspects.MyLoggingAspect  - around advise after int com.packt.ch03.dao.BookDAO.addBook(Book) B.L.method getting invoked 
1403 [main] INFO  com.packt.ch04.aspects.MyLoggingAspect  - int com.packt.ch03.dao.BookDAO.addBook(Book) took 388 to complete 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.70.203