Spring Certified Professional 2023 · Free Practice Question Easy
Question 24
Which pointcut expression in Spring AOP matches join points for method execution?
-
A
args
-
B
within
-
C
target
-
D
execution
Reveal correct answer
Correct answer: D
Explanation
Spring AOP provides different types of Pointcut expressions to specify which methods or join points should be advised. Some of the commonly used pointcut expressions are:
Execution - matches join points for method execution.
Within - matches join points within certain types or packages.
This - matches join points where the bean reference is an instance of a given type.
Target - matches join points where the target object is an instance of a given type.
Args - matches join points where the arguments to the method being executed match a given type or expression.
Annotation - matches join points where the subject has the given annotation.
These pointcut expressions can be used alone or in combination to create more complex pointcut expressions.
Here's an example of the execution pointcut expression in Spring AOP:
- lessCopy code
- @Aspect
- @Component
- public class MyAspect {
- @Before("execution(* com.example.myapp.service.UserService.getUser(..))")
- public void beforeGetUser() {
- System.out.println("Before advice executed for getUser method");
- }
- }
In this example, we define an aspect named MyAspect that intercepts calls to the getUser method in the UserService class. The @Before annotation indicates that the method should be executed before the target method is invoked, and the execution pointcut expression specifies the exact method signature to intercept.
https://docs.spring.io/spring-framework/docs/2.0.x/reference/aop.html#aop-pointcuts
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
