Spring Aspect Oriented Programming (AOP)

29. What is AOP? 
AOP that stands for Aspect Oriented Programming is a way to modify existing classes in a code base to change their behavior based on rules defined separately. This modification can be done before the classes are packaged into a JAR or WAR, or can happen dynamically while the code is being loaded. 
Rather than finding all the points of code that you want to modify in the source code and hard coding them, you just define the rules for how to find points of interest in the code and what changes you would like to do to them. These rules are called aspects – the A of AOP. 

30. What is Aspect? 
Consider a use case where an end user enters login and password, goes to the home page, performs some action, and returns back. 
The implementation code would typically consist of an HTML client that in turn would send the message to a servlet, which would perform the action. Now as you implement additional features, such as logging and transacting, your implementation code would just get bloated more. And assuming that features like logging or transaction management would have to be implemented across all modules, this would just make the code redundant, and maintenance much more difficult. This is where you can use aspect. 
When you have common concerns, such as logging, and exception handling that needs to be used across multiple objects, you can put it into one single module, known as an aspect that can be used across all other objects. 
Aspects are Java classes configured in a configuration file. Spring AspectJ provides the @Aspect annotation to declare a class as an aspect. 
31. What is the difference between concern and cross-cutting concern in Spring AOP? 
Concern means logic or functionality. Concern is a behaviour you want to have in a module of your application. A Concern may also be defined as a functionality that you want to implement to solve a specific business problem. 
On the other hand, a cross-cutting concern is a concern or a common functionality that spans across tiers and layers. For example, logging, security, and data transfer are the concerns which are needed in almost every module of an application and therefore may be considered as cross-cutting concerns. 
32. What is advice, joinpoint, pointcut and advice arguments in AOP? 
Advice: Advice is the actual action that will be taken either before or after the method execution. This is the actual piece of code that is invoked during the program execution by the Spring AOP Framework. It is the implementation of cross-cutting concern which you are interested in applying to other modules of your application.
Joinpoint: A joinpoint is a candidate point in the program execution of the application where an aspect can be plugged in. This joinpoint could be a point where a method is called, an exception thrown, or even a field being modified. At these points, you can insert your aspect’s code between the normal flow of your application to add a new feature or say behavior. 
Pointcut: A pointcut tells at what join points an advice should be applied. Advice can be applied at any joinpoint supported by the AOP framework. You can specify these pointcuts using Class and method names or through regular expressions that define the matching class and method name patterns. 
Advice Arguments: Advice arguments are the parameters that are passed to advice methods. You can use joinpoint as a parameter and get the method signature or the target object. You can use args() expression in the pointcut to be applied to any method that matches the argument pattern. If you use this, then you need to use the same name in the advice method from where argument type is determined.  

Comments

Popular posts from this blog

Spring Annotations

Spring Data Access

Spring Model View Controller (MVC)