Spring Boot Aspect-Oriented Programming Questions and Answers — Questions and Answers
Question 1: In a Spring Boot application, which AOP advice type provides the most control over a method's execution, allowing you to modify arguments, control whether the method proceeds, and alter the return value?
- @Before
- @AfterReturning
- @Around (Correct answer)
- @AfterThrowing
Correct answer: @Around
@Around advice is the most powerful because it wraps the target method call. It requires a ProceedingJoinPoint argument, which allows the advice to control the execution of the target method via the proceed() method. This enables pre-processing before the method runs, post-processing after, and even skipping the method execution entirely.
Question 2: A developer needs to apply logging to all public methods within any class in the `com.example.service` package. Which of the following is the correct AspectJ pointcut expression for this scenario?
- execution(* com.example.service.*.*(..))
- within(com.example.service.*)
- execution(public * com.example.service.*.*(..)) (Correct answer)
- target(com.example.service.*)
Correct answer: execution(public * com.example.service.*.*(..))
The expression 'execution(public * com.example.service.*.*(..))' correctly targets the execution of all public methods in any class within the specified package. 'public' specifies the access modifier. The first '*' is a wildcard for any return type. 'com.example.service.*' matches any class in the package. The second '.*' matches any method name, and '(..)' matches any number of parameters.
Question 3: What is the fundamental difference between a Join Point and a Pointcut in Spring AOP?
- A Join Point is the execution of advice, while a Pointcut is the method being advised.
- A Join Point is a potential point in the code where advice can be applied (like a method execution), while a Pointcut is an expression that selects one or more Join Points. (Correct answer)
- A Pointcut is a specific occurrence of a Join Point during the program's execution.
- They are interchangeable terms for the same concept in AOP.
Correct answer: A Join Point is a potential point in the code where advice can be applied (like a method execution), while a Pointcut is an expression that selects one or more Join Points.
A Join Point is a specific point during the execution of a program, such as a method execution. In Spring AOP, this always represents a method execution. A Pointcut is a predicate or expression that matches these join points, effectively selecting where the advice should be applied.
Question 4: A developer has an aspect with `@Around` advice that measures the execution time of a service method. If the developer forgets to call `ProceedingJoinPoint.proceed()` inside the advice, what will be the outcome?
- The application will throw a runtime exception.
- The advice will execute, but the target service method will not. (Correct answer)
- The target service method will execute, but the advice's post-processing logic will be skipped.
- The AOP proxy will fail to be created, causing a startup error.
Correct answer: The advice will execute, but the target service method will not.
The `ProceedingJoinPoint.proceed()` method is what actually invokes the target (advised) method. If it is not called within an `@Around` advice, the execution flow never reaches the target method. The advice will run its course, but the primary logic of the method it surrounds will be completely skipped.
Question 5: Under which circumstance will Spring AOP default to using a CGLIB proxy instead of a JDK dynamic proxy?
- When the target bean is declared with a `@Component` annotation.
- When the target bean to be proxied does not implement any interfaces. (Correct answer)
- When the application is configured with `spring.aop.proxy-target-class=false`.
- When the target bean implements only one interface.
Correct answer: When the target bean to be proxied does not implement any interfaces.
Spring AOP uses two types of proxies. If the target object implements at least one interface, a JDK dynamic proxy will be created, implementing the same interfaces. If the target object does not implement any interfaces, Spring will create a CGLIB proxy by creating a subclass of the target class at runtime.
Question 6: How do you enable AspectJ support in a modern Spring Boot application that uses Java configuration?
- By adding the `<aop:aspectj-autoproxy/>` element to an XML configuration file.
- By annotating a configuration class with `@EnableAspectJAutoProxy`.
- It is enabled by default if `spring-boot-starter-aop` is on the classpath; no explicit annotation is needed. (Correct answer)
- By creating a bean of type `AspectJWeaver` in the application context.
Correct answer: It is enabled by default if `spring-boot-starter-aop` is on the classpath; no explicit annotation is needed.
In Spring Boot, the `AopAutoConfiguration` class automatically enables AspectJ support if AOP dependencies, like `spring-boot-starter-aop`, are present on the classpath and a class is annotated with `@Aspect`. Therefore, explicit use of `@EnableAspectJAutoProxy` is not typically required.
In a Spring Boot application, which AOP advice type provides the most control over a method's execution, allowing you to modify arguments, control whether the method proceeds, and alter the return value?