You are reading the article Spring Aop Terminologies And Need For Spring Aop updated in September 2023 on the website Cersearch.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Spring Aop Terminologies And Need For Spring Aop
Introduction to Spring AOPAOP (Aspect Oriented Programming) introduces a new way of visualizing the programming structure. Like, OOP (Object-Oriented Programming), whose main unit of modularity is class, the unit of modularity for AOP is an aspect. Spring Framework also provides Aspect-Oriented Programming by implementing AOP concepts. Spring AOP is widely used as an implementation of cross-cutting concern i.e., a module or a functionality which is defined in one place but needed in many places across the project.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
In simple terms, the cross-cutting concern is something that is centralized in one place of the project & used across multiple places such as Logging, Authentication, Security, Transaction Management, etc.
Need for Spring AOPTo understand the need for Spring AOP in a better way, let’s look at a problem statement in brief & also how Spring AOP resolved it.
Problem StatementTo achieve this, we need to call the notification methods during the start & end in all the 5 methods in the service layer.
So, this is a tedious & repetitive work of writing the same code again & again in all the 5 methods. Also, in the future, if we wanted to remove the notification event for one of the methods, we need to remove the code again. So, there will a problem with maintenance too.
Code Snippets
So, if you notice in TransactionService class, for each & every method we are calling startService() and endService() method from NotificationService. This acts as a boilerplate coding & repetitive work for the developers. Also, if the client requires to stop calling the NotificationService for emailService() and loggingService(), again we need to remove the code from TransactionService class.
SolutionWith the help of Spring AOP, the aspect of calling the NotificationService during the start & end of each method can be centralized which reduces the repetitive work & also will be a good fit for the future with less maintenance.
So, Spring AOP dynamically provides a way to add cross-cutting concerns (i.e., calling the Notification Service in this case) using simple pluggable XML Configuration files or by using Java Annotations.
Spring AOP TerminologiesSome of the terminologies of Spring AOP are as follows.
Aspect
Pointcut
Advice
JoinPoint
Weaving
It would be difficult to understand the above terminologies theoretically. So, let us witness a practical example of using Spring AOP and how the terminologies are interconnected.
Spring AOP Code Snippet
Transaction Service (package: com.muneer.service)
From the above code snippet, Logging Management needs to be achieved during the start & completion for each & every method in TransactionService.
So, we are making use of Spring AOP to centralize the Logging Management.
1. AspectAspect is nothing but the concern or the functionality that you are trying to implement generally or centrally.
From the above example, the aspect is Logging Management.
Some of the aspects used across the industry are Logging Management, Transaction Management, Exception Handling, Performance Metrics, Authentication, Security, etc.,
In Simple terms, aspect is the functionality that you are trying to achieve through Spring AOP.
2. PointcutA pointcut is nothing but, a kind of regular expression that specifies, what are the method calls that need to be intercepted.
From the code snippet, Line no. 24 & 30 in AspectConfiguration,
@Before("execution(* com.muneer.service.*.*(..))")The Regular expression denoting that all the methods (using *.*) in package com.muneer.service needs to be intercepted. Also, @Before specifies, that logging needs to happen once the control enters into the method & @After specifies, that logging needs to happen once the control exits the method.
3. AdviceAdvice is nothing but, what are the action that needs to be done when a Pointcut is met.
From the code snippet, Line no. 27 & 33 in AspectConfiguration,
logger.info("Started Executing " + joinPoint.getSignature().getName());So here the Advice is, to start logging the information along with the method name when a Pointcut is met.
4. JoinPointsIt is not limited to only execution of methods, but also when an exception is thrown during Exception Handling Management.
5. WeavingWeaving is nothing but when a Pointcut is met, the corresponding method will get executed.
From the code snippet, Line no. 24 & 25 – When Pointcut is met, it makes sure to execute before() method. This process of binding the Pointcut with the method is referred to as Weaving.
Advantages of Spring AOP
As an implementation of cross-cutting concerns, functionalities such as Logging, Notification Management, Authentication, Security, Transaction Management can be kept in a centralized manner & can be used across multiple places in the application.
As AOP is implemented using Java, there is no need for any special compilation unit or class loaders.
As the cross-cutting concerns are centralized, Boilerplate coding is reduced.
It becomes easy for the developers to maintain the system.
Examples of Spring AOPLet us look at a real-time example with additional features such as printing the method parameters & return value with Spring AOP.
Code Snippets
Output
Conclusion Recommended ArticlesYou're reading Spring Aop Terminologies And Need For Spring Aop
Update the detailed information about Spring Aop Terminologies And Need For Spring Aop on the Cersearch.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!