Spring Interview Module 2 AOP Flashcards
What is the concept of AOP?
AOP – Aspect Oriented Programming – A programming paradigm that complements Object-oriented Programming (OOP) by providing a way to separate groups of crosscutting concerns from business logic code. This is achieved by ability to add additional behavior to the code without having to modify the code itself. This is achieved by specifying:
Location of the code which behavior should be altered – Pointcut is matched with Join point
Code which should be executed that implements cross cutting concern – Advice
Which problem does AOP solve?
Aspect Oriented Programming solves following challenges:
Allows proper implementation of Cross-Cutting Concerns
Solves Code Duplications by eliminating the need to repeat the code for functionalities across different layers, such functionalities may include logging, performance logging, monitoring, transactions, caching
Avoids mixing unrelated code, for example mixing transaction logic code (commit, rollback) with business code makes code harder to read, by separating concerns code is easier to read, interpret, maintain
Common cross-cutting concerns:
Logging
Performance Logging
Caching
Security
Transactions
Monitoring
What two problems arise if you don’t solve a cross cutting concern via AOP?
Implementing cross-cutting concerns without using AOP, produces following
challenges:
Code duplications – Before/After code duplicated in all locations when normally Advise would be applied, refactoring by extraction helps but does not fully solve the problem
Mixing of concerns – business logic code mixed with logging, transactions, caching makes code hard read and maintain
What is a join point in AOP?
Join Point in aspect oriented programming is a point in execution of a program in which behavior can be altered by AOP.
In Spring AOP Join Point is always method execution.
Aspect Oriented Programming concept in general, distinguishes additional Join Points, some of them include:
Method Execution / Invocation
Constructor Execution / Invocation
Reference / Assignment to Field
Exception Handler
Execution of Advice
Execution of Static Initializer / Object Initializer
self-invocation of method is not a join point in Spring AOP
What is a pointcut in AOP?
Pointcut is a predicate used to match join point. Additional code, called Advice is executed in all parts of the program that are matching pointcut. Spring uses the AspectJ pointcut expression language by default.
Example of Pointcut Expressions:
execution - Match Method Execution
execution(com.spring.professional.exam.tutorial.module02.question02.bls.CurrencyService.getExchangeRate(..))
within - Match Execution of given type or types inside package
within(com.spring.professional.exam.tutorial.module02.question02.bls.)
@within – Match Execution of type annotated with annotation
@within(com.spring.professional.exam.tutorial.module02.question02.annotations.Secured)
@annotation – Match join points where the subject of the join point has the given annotation
@annotation(com.spring.professional.exam.tutorial.module02.question02.annotations.InTransaction)
what is advice in AOP?
Advice is additional behavior that will be inserted into the code, at each join point matched by pointcut
@Before(“this(com.spring.professional.exam.tutorial.module02.question02.bls.CurrenciesRepositoryImpl)”)
public void beforeThisCurrenciesRepository() {
System.out.println(“Before - this(CurrenciesRepositoryImpl)”);
}
what is aspect in AOP?
Aspect brings together Pointcut and Advice. Usually it represents single behavior implemented by advice that will be added to all join points matched by pointcut.
Aspect = pointcut(where) + advice (what to do)
what is weaving in AOP?
Weaving is the process of applying aspects, which modifies code behavior at join points that have matching pointcuts and associated advices. During weaving aspects and application code is combined which enables execution of cross-cutting concerns.
Types of weaving:
Compile Time Weaving – byte code is modified during the compilation, aspects are applied, code is modified at join points matching pointcuts by applying advices
Load Time Weaving – byte code is modified when classes are loaded by class loaders, during class loading aspects are applied, code is modified at join points matching pointcuts by applying advices
Runtime Weaving – used by Spring AOP, for each object/bean subject to aspects, proxy object is created (JDK Proxy or CGLIB Proxy), proxy objects are used instead of original object, at each join point matching pointcut, method invocation is changed to apply code from advice
How does Spring solve
(implement) a cross cutting concern?
Spring Implements cross-cutting concerns with usage of Spring AOP module. Spring AOP uses AspectJ expression syntax for Pointcut expressions, which are matched against Join Point, code is altered with logic implemented in advices. In Spring AOP Joint Point is always method invocation.
Spring AOP uses Runtime Weaving, and for each type subject to aspects, to intercepts calls, spring creates one type of proxy:
JDK Proxy – created for classes that implements interface. proxy class implements interface and invoke original implementation class methods
CGLIB Proxy – created for class that are not implementing any interface. proxy class extends original class directly and invoke super.method of original class
It is possible to force Spring to use CGLIB Proxy with usage of
@EnableAspectJAutoProxy(proxyTargetClass = true)
Which are the limitations of the two
proxy-types?
JDK Dynamic Proxy Limitations:
Does not support self-invocation (aspect not executed )
Class must implement interface
Only method implementing the interface will be proxied (method in impl class but not in interface cannot execute aspects)
CGLIB Proxy Limitations:
Does not support self-invocation (aspect not executed )
Class for which proxy should be created cannot not be final (java forbids final class to be extended)
Method which should be proxied cannot be final (aspect not executed)
Only public/protected/package methods will be proxied, private methods are not proxied
What visibility must Spring bean methods have to be proxied using Spring AOP?
Spring Bean Method needs to have following visibility level to be proxied:
JDK Dynamic Proxy – public
CGLIB Proxy – public/protected/package
On top of requirement above, for call to be proxied, it needs to come from outside, both JDK Dynamic Proxy and CGLIB proxy does not support self-invocation.
How many advice types does
Spring support. Can you name each one?
Spring supports following advice types:
@Before – executed before joint point matched by pointcut is executed
@After – executed after joint point matched by pointcut is executed
@AfterThrowing – executed when exception is thrown from joint point matched by pointcut
@AfterReturning – executed after joint point matched by pointcut is executed successfully without any exception
@Around – allows you to take full control over joint point matched by pointcut, most powerful advice, allows you to implement all advices from above, you need to call ProceedingJoinPoint::proceed() to execute original code
what are different advice types used for?
Some examples of usage for each Advice type:
@Before
Authorization, Security
Logging
Data Validation
@After
Logging
Resource Cleanup
@AfterThrowing
Logging
Error Handling
@AfterReturning
Logging
Data Validation for method result
@Around
Transactions
Distributed Call Tracing
Authorization, Security
Which two advices can you use if you would like to try and catch exceptions?
To catch exceptions you can use two advices:
@AfterThrowing with throwing field set and exception passed as argument
@Around with try … catch block implemented