10 Hybris Spring AOP
10 Hybris Spring AOP
• 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.
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
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?
AOP - helps you decouple cross-cutting concerns from the objects that they affect.
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
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