22-AOP Flashcards

1
Q

What are cross-cutting concerns? Give some examples

A
Generic functionality that is needed in many places in your application
Examples:
1. Logging and tracing
2. Security
3. Custom Business Rules
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does Spring-AOP enable?

A
Modulization of cross-cutting concerns
i.e. The code for a cross-cutting concern is in a single place, in a module - in Java, a class represents a module
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’re the problems if failing to modularize cross-cutting concerns?

A
  1. Code tangling i.e. coupling of concerns

2. Code scattering i.e. the same concern spread across modules

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

What is aspect weaving?

A

Technique by which aspects are combined with main code

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

What library does Spring AOP use?

A

AspectJ

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

What is a join point?

A

A point in the execution of a program such as a method call or exception thrown

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

What is a pointcut?

A

An expression that selects one or more Join Points

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

What is an Advice?

A

Code to be executed at each selected JoinPoint

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

What is an Aspect?

A

A module that encapsulates pointcuts and advice

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

What is AOP proxy?

A
An “enhanced” class that stands in place of your original.
With extra behavior (Aspect) added (woven) into it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write an aspect component to advise all functions start with set and return void

A
@Aspect @Component
public class Tracker {
   @Before("execution(void set*(*))")
   public void method() {}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

List out all @EnableXXX annotations

A

@EnableAspectJAutoProxy

  • @EntityScan w/SpringBootApplication
  • @EnableJpaRepository(basePackage=”…”) with @Configuration
  • @EnableWebSecurity with @Configuration
  • @EnableGlobalMethodSecurity with
  • @AutoConfigureMockMvc with @SpringBootTest
  • @AutoConfigureTestDatabase
  • @EnableHypermediaSupport(type=HypermediaType.HAL) with @Configuration
  • @EnableWebMvc with @Configuration
  • @EnableAutoConfiguration
  • @EnableConfigurationProperties
  • @EnableTransactionManager
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is @EnableAspectJAutoProxy equivalent to?

A

XML: aop:aspectj-autoproxy

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

What aop:aspectj-autoproxy supports but @EnableAspectJAutoProxy doesn’t?

A

it has aop:include to sepecify ids if beans to use in aspect –> doesn’t need to scan all aspect components. But Spring AOP doesn’t have this

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

How to enable AOP in Spring app?

A

no need for Spring Boot app. Need to add @Configuration @EnableAspectJAutoProxy for non-Spring boot app

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

When is the point-cut expression used?

A
  1. “weaving” aspect and target

2. on every invocation, to decide if the advice should actually apply

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

List out pointcut designators

A

within: package

execution

18
Q

How to chain pointcuts together?

A

Using: && or || or !

19
Q

What is the method pattern in execution(method-pattern)?

A

[Modifiers] ReturnType [ClassType]

MethodName (Arguments) [throws ExceptionType]

20
Q

What are mandatory in pointcut method pattern?

A

[Modifiers] ReturnType [ClassType]
MethodName (Arguments) [throws ExceptionType]

21
Q

Can the pointcut work with the private/protected method in class?

A

yes if using CGLIB proxies

22
Q

Using pointcut, how to match method with 0, 1, 2 and any arguments

A

0: set()
1: set
()
2: set
(*, )
any: set
(..)

23
Q

The powerful match-all pointcut expression

A
  • *(..)
24
Q

Pointcut: match method starts with send, having an argument of type rewards.Dining and return void

A

execution(void send*(rewards.Dining))

25
Q

Pointcut: match method send, doesn’t care about return type but the first argument must be int. Can have 1+ argument

A

execution(* send(int, ..))

26
Q

Will this match MessageServiceImpl.send?

execution(void example.MessageService.send(*))

A

Yes. Matching including sub-class

27
Q

Pointcut to match all methods with @Logger

A

execution( @Logger * *(..) )

28
Q

Pointcut: match zero or more packages

A

*..

should not be .. because an expression with a leading .. does not work - hence *..

29
Q

Pointcut: match 1 directory between rewards and restaurant

A

execution(* rewards..restaurant..*(..))

30
Q

Pointcut: match several directories between rewards

and restaurant

A

execution(* rewards..restaurant..(..))

31
Q

Pointcut: Any sub-package called restaurant

A

execution(* ..restaurant..*(..))

32
Q

Write and advice function to print out the name of the joinpoint, first args, and name of the bean (having that joinpoint)

A

void adviceFunction(JoinPoint jp) {}

  1. Name of joinpoint: jp.getSignature().getName()
  2. arg: jp.getArgs()[0]
  3. bean name: jp.getTarget().toString()
33
Q

Meaning of * in pointcut

A

matches once (return type, package, class, method name, argument)

34
Q

Meaning of .. in pointcut

A

matches zero or more (arguments or packages)

35
Q

Define an advice & pointcut to re-thrown all IOException to MyIOException and wrap the original exception inside

A
@AfterThrowing(value="execution(* *(..) throws IOException", throwing="e")
void fun(JoinPoint jp, IOException e) {
    throw new MyIOException(e);
}

Note: The @AfterThrowing advice will not stop the exception from propagating

36
Q

Write advice function for @Around pointcut

A
Object adviceFn(ProceedingJoinPoint jp) throws Throwable {
    return jp.proceed();
}

Note: ProceedingJoinPoint not JointPoint
must throws Throwable because of jp.proceed()

37
Q

Limitation of Spring AOP

A
  1. can only advise non-private methods
  2. apply for Spring Beans only
  3. Limitation of weaving proxies
    When using proxies, suppose method a() calls method b() on the same class/interface
    advice will never be executed for method b()
38
Q

How to force CGLib proxy?

A

XML:

aop: config proxy-target-class=”true”
aop: aspectj-autoproxy proxy-target-class=”true”

39
Q

Pointcut to bind target bean and argument to arguments in the advice method

A
@Before("execution(something) && target(server) && args(host, port)")
void listen(Server server, String host, int port) {}
40
Q

Pointcut to bind annotation to argument in the advice method

A
@Before("execution(something) && @annotation(txn)")
void listen(Transactional txn) {}
41
Q

In Spring AOP, ________ can be of multiple types, such as “around”, “before” and “after”.

A

Advice