0% found this document useful (0 votes)
48 views4 pages

Understanding Spring AOP Concepts

AOP (Aspect-Oriented Programming) is a programming paradigm that allows separating cross-cutting concerns from the main code. Spring AOP and AspectJ are common AOP frameworks. Spring AOP uses proxies behind the scenes to intercept method executions using either JDK or CGLIB proxies. Aspects contain advice that implement cross-cutting concerns and pointcuts that define where advice applies. Weaving combines aspects with code to create proxied objects.

Uploaded by

ravikanchu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views4 pages

Understanding Spring AOP Concepts

AOP (Aspect-Oriented Programming) is a programming paradigm that allows separating cross-cutting concerns from the main code. Spring AOP and AspectJ are common AOP frameworks. Spring AOP uses proxies behind the scenes to intercept method executions using either JDK or CGLIB proxies. Aspects contain advice that implement cross-cutting concerns and pointcuts that define where advice applies. Weaving combines aspects with code to create proxied objects.

Uploaded by

ravikanchu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is AOP (Accept-Oriented Programming)?

• AOP framework is used to modularize cross-cutting concerns (like security, logging, transactions,
caching, exception handling etc.) in aspects. In simple words, it’s just an interceptor to intercept some
processes.
For example, when a method is executing, AOP can hijack the executing method, and add extra
functionality before or after the method execution.
• AOP introduces loose coupling between core and cross-cutting concerns
What are different AOP frameworks?

▪ Spring AOP
▪ AspjectJ
▪ JBoss AOP
Spring AOP and AspectJ are famous frameworks.

Spring AOP AspectJ


Learning curve is more Easier to learn
Less powerful (supports method execution More powerful (supports all pointcuts)
pointcut only)
Little runtime overhead Less runtime overhead compare to spring AOP
No special compiler need to build AspjectJ compiler needed to build
Weaving at runtime Weaving at compile time, load time or runtime

What is Proxy?

• Is an object that looks like another object, but adds special functionality behind the scenes
• Is well-used design pattern
• Sits in between the caller of an object and the real object itself
Example – Credit card or check is a proxy for the bank account

How spring AOP works behind the scenes?

• Spring AOP uses proxies behind the scenes.


• Spring provides 2 different options to create proxies at runtime
▪ CGLIB (Code Generation Library) proxy
▪ Is based on classes (proxy will become subclass of the target class)
▪ The class need to provide a default constructor
▪ Doesn’t work with final methods (because the proxy sub class cannot override
the class implementation)
▪ Is a third-party framework
▪ JDK proxy
▪ Is based on interfaces
▪ Default (spring aop uses this proxy as default)
▪ Comes out of the box in JDK

Contact Us = ChennaReddyTraining@[Link]
• Spring uses JDK dynamic proxy by default if interface exists. If there is no interface, then it will use
CGLIB proxy

Out of all frameworks Byte Buddy is a most modern proxy framework with precise documentation and
with various examples. Mockito used CGlib over years, in recent time mockito is replacing CGLIB by
Byte Buddy.

AOP Terms
Aspect - Is a concern (cross cutting concern) that you want to implement in the application. An aspect contains
number of advices and pointcuts
Example - logging, security, transaction, caching, exception etc. are the aspects

Advice = Is the actual implementation of the aspect. Aspect is a concept and Advice is the concrete
implementation of the concept (is a method that addresses the part of concern)

Join Point = A point in the java program where the advice need to be applied (before, after, after returning,
around etc.). Spring AOP supports only method level point cuts

Pointcut = Collection of joint points and provides syntax to express joint points

Weaving = Is a process / technique by which aspects are combined with main code to create new proxied
object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime.
Spring AOP performs weaving at runtime.

Contact Us = ChennaReddyTraining@[Link]
How to force spring to use CGLIB proxy?

By setting proxy-target-class to true in the aop:config element


<aop:config proxy-target-class="true">
...
</aop:config>

What are different Types of AOP advices?


(1) Before advice = Run before the method execution
(2) After returning advice = Run after the method returns a result
(3) After (finally) advice = Run after the method execution regardless of the its outcome
(4) After throwing advice = Run after the method throws an exception
(5) Around advice = Run around (before or after) the method execution

What is the difference between DI (Dependency Injection) and AOP?

DI - helps you decouple your application objects from each other

AOP - helps you decouple cross-cutting concerns from the objects that they affect.

What are the differences between OOP and AOP?

AOP OOP
Aspect – code unit that encapsulates point Class – code unit that encapsulates the
cuts, advice and attributes methods and attributes
Pointcut – defines the set of entry points in
Method signature – defines the entry
which advice is executed points for the execution of method bodies
Advice – implementation cross cutting concern
Method bodies – implementation of the
business logic concerns
Weaver – construct code (source or object) Compiler – convert source code to object
with advice code

@Retention = This annotation indicates how long the annotation need to be retained. There are 3 different
types of retention policies.
1) SOURCE – retained only in the source file and is discarded during compilation.
Examples - @Override, @SupressWarnings
2) CLASS (Default annotation) – stored in the .class file during compilation, not available in the run
time. Useful when doing byte code level processing.
3) RUNTIME - stored in the .class file and available in the run time and hence can be reflectively
found on a class.
Examples - @Deprecated

Contact Us = ChennaReddyTraining@[Link]
Pointcut syntax

Syntax for different Types of advices

ProceedingJoinPoint = Is used around advice that can control when and if a method (or other join point) is
executed. This is true for around advices only, so they require an argument of type ProceedingJoinPoint,
whereas other advices just use a plain JoinPoint

Note: - [Link]() will call the real method it arounds


Contact Us = ChennaReddyTraining@[Link]

Common questions

Powered by AI

Join points specify well-defined points in a program's execution, such as method calls or executions, where additional behavior can be inserted. Pointcuts are expressions that select one or more join points, allowing developers to target specific points of interest to apply cross-cutting aspects. This separation of where (join points) and what (advice) significantly enhances efficiency, enabling precise application of cross-cutting concerns without manual intervention. By using pointcuts to target necessary join points, AOP ensures high flexibility in managing cross-sectional functionality, while keeping the business logic intact.

The proxy pattern is central to enabling AOP within Spring, facilitating the dynamic interception of method invocations to apply cross-cutting concerns like logging or security checks without modifying the actual method code. This design pattern supports cleaner, modular applications by isolating aspects from core business logic. It reduces coupling between cross-cutting concerns and business functionality, enhancing code maintainability and testing. However, designing applications with proxies may introduce complexity in understanding control flows and debugging due to indirect method executions. Thus, careful design considerations are needed to prevent performance overhead and maintainability issues.

Advice in AOP separates the cross-cutting concerns from the business logic by allowing functionality such as logging or transaction management to be modularized in distinct advice classes. This abstraction enables cleaner, more maintainable code as developers can modify the cross-cutting logic without affecting the core business operations. By invoking the advice at designated join points, AOP facilitates changes to cross-cutting functionalities without needing to alter the existing business logic, enhancing maintainability and modularization.

The proxy pattern allows an object, known as the proxy, to control access to another object. This proxy can introduce additional functionalities such as logging or security checks transparently before or after the real object's methods are invoked. In Spring AOP, proxies are used to intercept method calls and apply advice, thus enabling the integration of cross-cutting concerns like transactions and logging. Spring AOP utilizes either JDK dynamic proxies (for interfaces) or CGLIB proxies (for classes) to implement this pattern.

Spring AOP is considered easier to learn because it provides method execution pointcuts only and requires less extensive configuration. However, it is less powerful compared to AspectJ, which supports all pointcuts, including more complex ones. AspectJ requires a special compiler and can offer a more robust integration via compile-time, load-time, or runtime weaving, while Spring AOP performs weaving only at runtime.

Annotations can be retained at different levels: SOURCE, CLASS, and RUNTIME. SOURCE retention means the annotation is only present in the source code and discarded during compilation. CLASS retention, the default, stores the annotation in the .class file but does not make it available at runtime. RUNTIME retention is crucial for AOP as it allows annotations to persist into runtime, facilitating reflection, which dynamic frameworks like Spring AOP use to apply aspects by examining these annotations during execution.

AspectJ offers greater capabilities compared to Spring AOP, supporting a broader range of pointcuts that include object construction and initializations, method calls, and more intricate control flows. This enables developers to apply advice more intricately throughout the application lifecycle, beyond method execution points, allowing deep cross-cutting logic integration. AspectJ's flexibility with compile-time, load-time, and runtime weaving makes it suitable for complex applications needing optimized performance and higher control over cross-cutting concerns. However, with these advantages comes a steeper learning curve and more complicated setups as compared to the simpler but limited Spring AOP.

Weaving in AOP is the process of injecting aspects into the target object to create a new, proxied object. It can occur at different stages: compile-time, load-time, or runtime. Compile-time weaving involves the AspectJ compiler to directly alter the Java bytecode. Load-time weaving incorporates aspects during the class loading process. Runtime weaving, which is used by Spring AOP, occurs dynamically and allows aspects to be applied to classes as they are being used in the application, without altering the original class files.

To force Spring to use CGLIB proxies, set the `proxy-target-class` attribute to true in the `<aop:config>` element of the Spring configuration file. This configuration is used when the target class does not implement any interfaces or when specific CGLIB features are desired, such as proxying classes rather than interfaces. The implication is that CGLIB will enhance the performance compared to JDK proxies and support classes without interfaces, but it requires subclassing the target class, which may impose some limitations like not being applicable to final classes or methods.

An aspect represents a modularization of a concern that cuts across multiple classes, usually covering broad functionalities such as logging or security. It is a concept encompassing several advices and related configuration like pointcuts. Advice, on the other hand, is the actual implementation within an aspect; it defines the action taken at a specific joint point, such as method start or completion. Advice specifies the code that runs when the join point is reached. While an aspect defines what should be done across different parts, advice is the specific implementation of those actions.

You might also like