Code scattering

This means that the same concern is spread across modules in the application. Code scattering promotes the duplicity of the concern's code across the application modules. Let's see the following code to understand more about code scattering:

    public class TransferServiceImpl implements TransferService { 
      public void transfer(Account a, Account b, Double amount) { 
        //Security concern start here 
        if (!hasPermission(SecurityContext.getPrincipal()) { 
          throw new AccessDeniedException(); 
        } 
        //Security concern end here 
          
        //Business logic start here 
        ... 
      } 
    } 
 
    public class AccountServiceImpl implements AccountService { 
      public void withdrawl(Account a, Double amount) { 
        //Security concern start here 
        if (!hasPermission(SecurityContext.getPrincipal()) { 
          throw new AccessDeniedException(); 
        } 
        //Security concern end here 
          
        //Business logic start here 
        ... 
      } 
    } 

As you can see in the preceding code, there are two modules for the application, TransferService and AccountService. Both modules have the same cross-cutting concern code for the security. The bold highlighted code in both business modules are the same, it means there is code duplication here. The following figure illustrates code scattering:

In the preceding figure, there are three business modules TransferService, AccountService, and BankService. Each business module contains cross-cutting concerns such as Security, Logging and Transaction management. All modules have the same code of concerns in the application. It is actually duplication of concerns code across the application.

Spring AOP provides solution for these two problems that is, code tangling and code scattering in the Spring application. Aspects enable modularization of cross-cutting concerns to avoid tangling and to eliminate scattering. Let's see in further section how AOP solves these problems.

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

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