Define aspects using Annotation

Suppose in your bank application, you want to generate log for a money transfer service for auditing and tracking to understand customers' behaviors. A business never succeeds without understanding its customers. Whenever you will think about it from the perspective of a business, an auditing is required but isn't central to the function of the business itself; it's a separate concern. Therefore, it makes sense to define the auditing as an aspect that's applied to a transfer service. Let's see the following code which shows the Auditing class that defines the aspects for this concern:

    package com.packt.patterninspring.chapter6.bankapp.aspect; 
 
    import org.aspectj.lang.annotation.AfterReturning; 
    import org.aspectj.lang.annotation.AfterThrowing; 
    import org.aspectj.lang.annotation.Aspect; 
    import org.aspectj.lang.annotation.Before; 
 
    @Aspect 
    public class Auditing { 
 
      //Before transfer service 
      @Before("execution(* com.packt.patterninspring.chapter6.bankapp.
service.TransferService.transfer(..))") public void validate(){ System.out.println("bank validate your credentials before
amount transferring"); } //Before transfer service @Before("execution(* com.packt.patterninspring.chapter6.bankapp.
service.TransferService.transfer(..))") public void transferInstantiate(){ System.out.println("bank instantiate your amount
transferring"); } //After transfer service @AfterReturning("execution(* com.packt.patterninspring.chapter6.
bankapp.service.TransferService.transfer(..))") public void success(){ System.out.println("bank successfully transferred amount"); } //After failed transfer service @AfterThrowing("execution(* com.packt.patterninspring.chapter6.
bankapp.service.TransferService.transfer(..))") public void rollback() { System.out.println("bank rolled back your transferred amount"); } }

As you can see how the Auditing class is annotated with @Aspect annotation. It means this class is not just Spring bean, it is an aspect of the application. And Auditing class has some methods, these are advices and define some logic within these methods. As we know that before beginning to transfer amount from an account to another, bank will validate (validate ()) the use credentials and after that instantiate (transferInstantiate()) this service. After successful validation (success ()) amount is transferred and the bank audits it. But if the amount transferring fails in any case, then the bank should roll back (rollback ()) that amount.

As you can see, all methods of Auditing aspects are annotated with advice annotations to indicate when those methods should be called. Spring AOP provides five type advice annotations for defining advice. Let's see in the following table:

Annotation

Advice

@Before

It is used for before advice, advice's method executes before the advised method is invoked.

@After

It is used for after advice, advice's method execute after the advised method executes normally or abnormally doesn't matter.

@AfterReturning

It used for after returning advice, advice's method execute after the advised method complete successfully.

@AfterThrowing

It used for after throwing advice, advice's method execute after the method terminate abnormally by throwing an exception.

@Around

It is used for around advice, advice's method executes before and after the advised method invoked.

 

Let's see the implementation of advices and how these work in the application.

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

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