A popular Java framework, Spring offers support for plenty of requirements of the Java app. It supports web services, dependency injection, and more. The main aim of a framework is to lift heavy jobs like database connectivity, Java web development service calls from developers; so that they can concentrate on developing the business logic of the application. Apart from this the framework also aims at reducing the redundant code in an application. This code redundancy occurs when we need to perform the same operation across different modules of an application. We call these operations that span across modules cross-cutting concerns.
There are some cross-cutting concerns in every application. For example, we may need to log the parameters of methods in all classes. Instead of inserting a repetition logging code, the Spring framework can be used. It offers the AOP for logging.
Here are the details of the aspect-oriented programming in the Spring framework:
Class is the unit of modularity in Object-Oriented Programming. A piece of code that is intended to perform a certain function. Aspect is the unit of modularity in the AOP. It allows the modularization of cross-cutting concerns within the app. AOP is an eminent part of the Spring Framework. Now let us see some basic concepts in AOP.
- Aspect: It is a POJO class. It has the logic and the logic is common across modules. @Aspect annotation is generally used to spot an aspect.
- Join point: It refers to the point in the app execution where the aspect can be applied. For example we can say the aspect needs to be called when a method invocation happens or an exception is thrown.
- Advice : This says when and where an aspect needs to be invoked. There are "around," "before" and "after" advice types. The actual execution of aspect logic happens here. The before advice applies before the joining point.
- Point cut: Point cuts are specified using expressions or patterns which say where an aspect is to be executed. Mostly, Advice is accompanied by a Pointcut expression. Also, it runs at any Joinpoint in correspondence with the Pointcut.
Add below maven dependencies for AOP in your applications pom.xml to use AOP in your Spring project.
Define an Aspect:
An aspect can be defined using @Aspect annotation in a POJO
The above code will declare LoggingAspect POJO as an aspect in your application. This can contain the logic for logging
Define a PointCut:
A point cut is defined using @Pointcut annotation.
The above pointcut definition indicates the execution of any method in any class of the package com.example.myapp.service, where we are going to apply the aspect. logServiceCalls() invocation is linked with the execution of various methods within the package.
The advices can be added using annotations @Before - , @After, @Around, @AfterReturning, @AfterThrowing etc.
The above code shows the use of a JoinPoint in the advice. It gives us the flexibility to control the application execution. A call to proceed() method of JoinPoint will trigger the serviceCall() invocation. The JoinPoint also has methods to get the parameters passed to the point cut method.
In this tutorial, we have seen how aspect-oriented programming can be used to handle cross-cutting concerns in an application. AOP aids the modularizing of the cross-cutting concerns. It also enables the ease of applying it to the method while executing. AOP offers a wide range of ways to interrupt the execution of the method. A lot of boilerplate code can be avoided by the judicious usage of AOP concepts. Thus AOP helps developers to write efficient code with ease.