Adding After Returning advice

Although we've written the After advice, we are not able to get the value returned from the business logic method. After returning will help us to get the returned value with the help of the following steps:

  1. Add a method, returnAdvise(), in MyLoggingAspect, which will get invoked for after returning. The code is as follows:
public void returnAdvise(JoinPoint joinPoint, Object val) { 
  logger.info(joinPoint.getSignature()+ "returning val" + val); 
} 

The argument val will hold the returned value.

  1. Configure the advice under myLogger. We don't have to configure the pointcut, as we will be reusing already configured. In case you want to use a different set of join point, first you need to configure a different pointcut expression. Our configuration will be as follows:
<aop:after-returning pointcut-ref="pointcut2" 
  returning="val" method="returnAdvise"/> 

In the preceding configuration, the attribute returning represents the attribute to specify the name of the parameter to which the return value will be passed. In our case, this name is val, which is bound in advice arguments.

  1. To make the output easy to understand, comment before and after advice configuration, and then execute MainBookDAO_operations.java to get the following lines on the console as output:
1378 [main] INFO  com.packt.ch04.aspects.MyLoggingAspect  - int com.packt.ch03.dao.BookDAO.addBook(Book)  
returning val:-1 
1426 [main] INFO  com.packt.ch04.aspects.MyLoggingAspect  - int com.packt.ch03.dao.BookDAO.updateBook(long,int) returning val:-1 
1475 [main] INFO  com.packt.ch04.aspects.MyLoggingAspect  - boolean com.packt.ch03.dao.BookDAO.deleteBook(long) returning val:-true 

Each of the preceding statements shows the value returned from the Joinpoint.

..................Content has been hidden....................

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