Spring Interview Module 2 AOP Flashcards

1
Q

What is the concept of AOP?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which problem does AOP solve?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Common cross-cutting concerns:

A

 Logging
 Performance Logging
 Caching
 Security
 Transactions
 Monitoring

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What two problems arise if you don’t solve a cross cutting concern via AOP?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a join point in AOP?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a pointcut in AOP?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is advice in AOP?

A

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)”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is aspect in AOP?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is weaving in AOP?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does Spring solve
(implement) a cross cutting concern?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which are the limitations of the two
proxy-types?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What visibility must Spring bean methods have to be proxied using Spring AOP?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How many advice types does
Spring support. Can you name each one?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what are different advice types used for?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which two advices can you use if you would like to try and catch exceptions?

A

To catch exceptions you can use two advices:
 @AfterThrowing with throwing field set and exception passed as argument
 @Around with try … catch block implemented

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What do you have to do to enable the
detection of the @Aspect annotation?

A

To enable detection of @Aspect annotation you need to:
 Have @Configuration class with @EnableAspectJAutoProxy
 Without @EnableAspectJAutoProxy Spring will not scan for @Aspect
 Have beans for @Aspect annotated classes created
 Use @ComponentScan with @Component at class annotated with @Aspect (option 1 of 2)
 Use @Bean in Configuration class and create Spring Aspect Bean manually (option 2 of 2)
 Have aspectjweaver/spring-aop on classpath
 It is easiest to use org.springframework:spring-aspects dependency to have those included
 Without required dependencies on classpath spring will fail with
ClassNotFoundException/NoClassDefFoundError during creation of Proxy objects for Spring Beans subject to aspects

17
Q

What does
@EnableAspectJAutoProxy do?

A

Annotation @EnableAspectJAutoProxy enables detection of @Aspect classes and creates proxy object for beans subject to aspects.

Internally process of creating proxies is done by AnnotationAwareAspectJAutoProxyCreator.

By creating a proxy for each bean subject to aspects, spring intercepts the calls and implements Before / After / AfterReturning / AfterThrowing / Around advices.

It is important to remember that @Aspect will not create Spring Beans on it’s own, you need to use Component Scanning or manually create beans for @Aspect classes.

18
Q

Pointcut designator types supported by Spring AOP:

A

Spring supports 10 types of pointcut designators
 execution
 within
 args
 bean
 this
 target
 @annotation
 @args
 @within
 @target

Pointcut expressions can be combined together with usages of logical operators:
 ! – negation
 || - logical or
 && - logical and

19
Q

what is Pointcut designator – execution?

A

Pointcut designator – execution – matches method execution
General Form:
execution([visibility modifiers] [return type] [package].[class].method [throws exceptions]

Description:
 [visibility modifiers] – public/protected, if omitted all are matched, can be used with negation, for
example !protected
 [return type] – void, primitive or Object type, cannot be omitted, can be used with wildcard *, can be used with
negation, for example !int
 [package] – package in which class is located, may be omitted if class is located within same package as aspect,
wildcard * may be used to match all packages, wildcard .. may be used to match all sub-packages
 [class] – Class name to match against, may be omitted, may be used with * wildcard, matches subclasses of the class as well
 [method] – Name of the method, whole or partial method name can be used with * wildcard
 [arguments] – May be empty to match methods without any arguments, may be used with wildcard .. to match zero or more arguments, may be used with wildcard * to match all types of specific argument, may be used with ! Negation
 [throws exceptions] – Match method that throws exceptions from given list, can be used with negation !

20
Q

what is Pointcut designator – within?

A

Pointcut designator – within – matches execution within specified class/classes, optionally you can specify class package

General Form:
within([package].[class])
Description:
 [package] – package where class is located, may be used with .. wildcard (includes
all sub-packages) or with * wildcard, may be omitted
 [class] – class against which match should happen, may be used with * wildcard

21
Q

what is Pointcut designator – within?

A

Pointcut designator – within – matches execution within specified class/classes, optionally you can specify class package

General Form:
within([package].[class])
Description:
 [package] – package where class is located, may be used with .. wildcard (includes
all sub-packages) or with * wildcard, may be omitted
 [class] – class against which match should happen, may be used with * wildcard

22
Q

what is Pointcut designator – args?

A

Pointcut designator – args – matches execution of method with matching arguments

General Form:
args([parameter_type1, parameter_type2, …, parameter_typeN])

Description:
 [parameter_typeN] – simple or object type, may be * to indicate one parameter of any type, may be .. to indicate zero or more arguments, you can specify type with the package

23
Q

what is Pointcut designator – bean ?

A

Pointcut designator – bean – matches execution of method with matching Spring Bean Name

General Form:
bean([beanName])

Description:
 [beanName] – name of the Spring Bean (automatically generated by framework, or set manually)

24
Q

what is Pointcut designator – this?

A

Pointcut designator – this – matches execution against type of proxy that was generated by Spring AOP
this refers to the created proxy bean.

General Form:
this([type])

Description:
 [type] – type of the proxy, matches if generated proxy is of specified type

25
Q

what is Pointcut designator – target?

A

Pointcut designator – target – matches execution against type of the target object invoked by proxy
target refers to the original bean of the proxy created from.

General Form:
target([type])

Description:
 [type] – type of the target object invoked by proxy, matches if target object is of specified type

26
Q

what is Pointcut designator – @annotation?

A

Pointcut designator – @annotation – matches method execution annotated with specified annotation

General Form:
@annotation([annotation_type])

Description:
 [annotation_type] – type of annotation used to annotated method which should match pointcut expression

27
Q

what is Pointcut designator – @args ?

A

Pointcut designator – @args – matches method execution with argument, which types (classes) are annotated with specified annotation type, note that class should be annotated, not the argument of method itself

General Form:
@args([annotation_type])

Description:
 [annotation_type] – type of annotation used on top of class, which represents type of argument

28
Q

what is Pointcut designator – @args ?

A

Pointcut designator – @args – matches method execution with argument, which types (classes) are annotated with specified annotation type, note that class should be annotated, not the argument of method itself

General Form:
@args([annotation_type])

Description:
 [annotation_type] – type of annotation used on top of class, which represents type of argument

29
Q

what is Pointcut designator – @within?

A

Pointcut designator – @within – matches method executions inside classes annotated with specified annotation

General Form:
@within([annotation_type])

Description:
 [annotation_type] – type of annotation used on top of class, inside which method execution should be matched

30
Q

what is Pointcut designator – @target?

A

Pointcut designator – @target – matches method executions inside proxied target class that is annotated with specific annotation

General Form:
@target([annotation_type])

Description:
 [annotation_type] – type of annotation used on top of proxied class, inside which method execution should be matched

31
Q

What is the JoinPoint argument used
for?

A

joinPoint is a representation object of the actual executed method invokation

JoinPoint argument is an object that can be used to retrieve additional information about join point during execution. JoinPoint needs to be the first parameter of Advice, only in that case Spring Framework will inject JoinPoint into advice method.

Join Point is supported in following advice types:
 Before
 After
 After Returning
 After Throwing

Examples of information that you can retrieve from JoinPoint:
 String representation of Join Point
 Arguments of Joint Point (for example Method Arguments)
 Signature of Joint Point (for example Method Signature)
 Kind / Type of Joint Point
 Target / This object being proxied

32
Q

What is a ProceedingJoinPoint? When
is it used?

A

ProceedingJoinPoint is an object that can be provided to @Around advice as first argument, it is a type of JoinPoint which can be used to change method arguments during method execution in runtime or block execution of original method entirely.

ProceedingJoinPoint is used in @Around advice, it contains all methods from JoinPoint and also adds:
 proceed – executes original method
 proceed(args) – executes original method with provided arguments

ProceedingJoinPoint can be used in following use cases:
 Conditionally block method execution
 Filter arguments
 Inject additional argument