Implementing after advice

Just like before advice, we can also implement other advice types. Let's take an example of after advice and around advice. For after advice, just add one method in the LoggingAspect class as follows:

 //After advice method.
public void printEndLog(JoinPoint joinPoint) {
System.out.println(" ****** End of Method '"+joinPoint.getSignature().getName());
}

In after advice, we are just printing the method name. We also need to update the aspect configuration in the application context file. Just add an after advice entry for our logging aspect, as follows:

<aop:aspect id="myLoggin" ref="loggingAspect">
<aop:before pointcut-ref="employeeServiceMethods" method="printStartLog"/>
<aop:after pointcut-ref="employeeServiceMethods" method="printEndLog"/>
</aop:aspect>


You will get the following output:

 
-----------------------------------
****** Starting Method 'generateSalarySlip' of class com.packet.spring.aop.service.EmployeeService
Generating payslip
****** End of Method 'generateSalarySlip

****** Starting Method 'showTotalEmployee' of class com.packet.spring.aop.service.EmployeeService
The string is -->test
****** End of Method 'showTotalEmployee

****** Starting Method 'findEmployee' of class com.packet.spring.aop.service.EmployeeService
finding employee based on employeeId
****** End of Method 'findEmployee

This time, you can observe how AOP intercepts each method and implements after advice along with before advice.

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

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