0
0
Spring Bootframework~10 mins

@After and @AfterReturning in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AgetUser
BUserService
Cexecution
DafterMethod
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.
2fill in blank
medium

Complete 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'
AreturnValue
Boutput
Cvalue
Dresult
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'.
3fill in blank
hard

Fix 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'
Aproduct
Bresult
Cvalue
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for 'returning' and the method parameter.
Forgetting to declare the parameter in the advice method.
4fill in blank
hard

Fill 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'
AUserService
BOrderService
Cservice
DUserService.getUser
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.
5fill in blank
hard

Fill 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'
AUserController
BgetUserDetails
Cresult
DfetchData
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between 'returning' attribute and method parameter name.
Using wrong class or method names in the pointcut.