Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an @After advice that runs after the method execution.
Spring Boot
@After("execution(* com.example.service.UserService.[1](..))") public void afterMethod() { System.out.println("Method finished."); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using class name instead of method name in the pointcut.
Leaving the method name blank.
Using the advice method name instead of the target method name.
✗ Incorrect
The @After annotation requires the method name in the pointcut expression. Here, 'getUser' is the method we want to advise after execution.
2fill in blank
mediumComplete the code to create an @AfterReturning advice that captures the returned value.
Spring Boot
@AfterReturning(pointcut = "execution(* com.example.service.OrderService.placeOrder(..))", returning = "[1]") public void afterReturningAdvice(Object [1]) { System.out.println("Returned: " + [1]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching the 'returning' attribute and method parameter names.
Using a name in the method parameter that is not declared in 'returning'.
✗ Incorrect
The 'returning' attribute name must match the parameter name in the advice method. 'result' is commonly used and matches both places.
3fill in blank
hardFix the error in the @AfterReturning advice by completing the missing part.
Spring Boot
@AfterReturning(pointcut = "execution(* com.example.repository.ProductRepository.findById(..))", returning = "[1]") public void logReturnValue(Object [1]) { if ([1] != null) { System.out.println("Product found: " + [1]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for 'returning' and the method parameter.
Forgetting to declare the parameter in the advice method.
✗ Incorrect
The 'returning' attribute and the method parameter must have the same name. 'result' is a common choice and fixes the mismatch error.
4fill in blank
hardFill both blanks to create an @After advice that runs after any method in the service package.
Spring Boot
@After("execution(* com.example.service.[1].*(..))") public void afterAnyMethod() { System.out.println("A service method finished: [2]"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using package name instead of class name in the pointcut.
Putting a method name instead of class name in the pointcut.
✗ Incorrect
The pointcut targets all methods in the UserService class inside the service package. The print statement uses the class name for clarity.
5fill in blank
hardFill all three blanks to create an @AfterReturning advice that logs the returned value and method name.
Spring Boot
@AfterReturning(pointcut = "execution(* com.example.controller.[1].[2](..))", returning = "[3]") public void logReturn(Object [3]) { System.out.println("Method [2] returned: " + [3]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between 'returning' attribute and method parameter name.
Using wrong class or method names in the pointcut.
✗ Incorrect
The pointcut targets the getUserDetails method in UserController. The returning attribute and parameter use 'result' to capture the return value.