What's AOP and how Spring uses it within its framework?
Spring in Action states about AOP as follows “Spring’s AOP framework allows you to code functionality that is sprinkled throughout your application in one place—an aspect”.
Say for example your application has several modules and you want to maintain a common set of functionality that cross cuts the modules like reusing common functionality. The common methdology is using technique that is part of OO concept (inheritance, delegates etc). But inheritance can lead to brittle object hierarchy that is used throughout the application across various modules and delegates can be cumbersome. This is where AOP comes handy and following aspect terminology describes the common concepts covered in AOP.
-
Aspect - Most commonly logging is quite crucial part of application and we can define our own logging aspect that can be applied to different modules (like common resuable code).
-
Jointpoint - Its like a plug that can be inserted into your code (for example., aspect can be aspet’s code can be inserted into your code to add a new behaviour).
-
Advice is actual implementation of aspect. It is advising your application of new behaviour. Logging advice would contain the code that implements the actual logging such as writing to a log file.
/* Spring Aspect */
Java annonatations are quite a powerful concept and there are several of them that are used in standard library.
@Retention - “Indicates how long annotations with the annotated type are to be retained.” [2] Used to mark another annotation.
@Aspect The aspect annotation
@Rentention(RentitionPolicy.Runtime) public interface Aspect { public String value() default “ “; }