0% found this document useful (0 votes)
156 views

10 Hybris Spring AOP

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)
156 views

10 Hybris Spring AOP

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
You are on page 1/ 4

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 = [email protected]
• 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 = [email protected]
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 = [email protected]
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: - pjp.proceed() will call the real method it arounds


Contact Us = [email protected]

You might also like