Bird
Raised Fist0
Spring Bootframework~10 mins

AOP for logging in Spring Boot - Interactive Code Practice

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

Complete the code to define a pointcut expression that matches all methods in the service package.

Spring Boot
@Pointcut("[1]")
public void serviceMethods() {}
Drag options to blanks, or click blank then click option'
Awithin(com.example.repository..*)
Bexecution(public void com.example.controller..*(..))
Cexecution(* com.example.service..*(..))
Dbean(*Service)
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong package name in the expression.
Using 'within' instead of 'execution' for method matching.
2fill in blank
medium

Complete the advice method to log before method execution using AOP.

Spring Boot
@Before("serviceMethods()")
public void logBefore(JoinPoint joinPoint) {
    System.out.println("[1]" + joinPoint.getSignature().getName());
}
Drag options to blanks, or click blank then click option'
A"Calling method: "
B"Finished method: "
C"Error in method: "
D"Starting method: "
Attempts:
3 left
💡 Hint
Common Mistakes
Logging after the method instead of before.
Using a message that implies the method finished.
3fill in blank
hard

Fix the error in the after returning advice to log the returned value.

Spring Boot
@AfterReturning(pointcut = "serviceMethods()", returning = "[1]")
public void logAfterReturning(JoinPoint joinPoint, Object [1]) {
    System.out.println("Method " + joinPoint.getSignature().getName() + " returned: " + [1]);
}
Drag options to blanks, or click blank then click option'
Aresult
BreturnValue
Cvalue
Doutput
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names in the annotation and method parameter.
Forgetting to declare the parameter in the method.
4fill in blank
hard

Fill both blanks to create an around advice that logs before and after method execution.

Spring Boot
@Around("serviceMethods()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println("[1]" + joinPoint.getSignature().getName());
    Object result = joinPoint.[2]();
    System.out.println("Finished method: " + joinPoint.getSignature().getName());
    return result;
}
Drag options to blanks, or click blank then click option'
AStarting method:
Bproceed
Cexecute
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong method name instead of proceed().
Logging after proceed() but forgetting to log before.
5fill in blank
hard

Fill all three blanks to define a logging aspect class with proper annotations and pointcut.

Spring Boot
import org.aspectj.lang.annotation.[1];
import org.aspectj.lang.annotation.[2];
import org.springframework.stereotype.[3];

@Aspect
@[3]
public class LoggingAspect {

    @Pointcut("execution(* com.example.service..*(..))")
    public void serviceMethods() {}

}
Drag options to blanks, or click blank then click option'
AAspect
BComponent
CBefore
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service instead of @Component.
Forgetting to import AspectJ annotations.

Practice

(1/5)
1. What is the main purpose of using AOP (Aspect-Oriented Programming) for logging in Spring Boot?
easy
A. To write logging code inside every method manually
B. To replace all existing logging frameworks
C. To add logging without changing the main business code
D. To disable logging for all methods automatically

Solution

  1. Step 1: Understand AOP's role in logging

    AOP allows adding extra behavior like logging without modifying the main code logic.
  2. Step 2: Identify the benefit of separation

    By using AOP, logging code is kept separate, making the main code cleaner and easier to maintain.
  3. Final Answer:

    To add logging without changing the main business code -> Option C
  4. Quick Check:

    AOP adds logging cleanly = A [OK]
Hint: AOP separates logging from business code [OK]
Common Mistakes:
  • Thinking AOP replaces logging frameworks
  • Believing logging must be written inside each method
  • Assuming AOP disables logging automatically
2. Which annotation is used in Spring AOP to execute code before a method runs for logging purposes?
easy
A. @AfterReturning
B. @AfterThrowing
C. @Around
D. @Before

Solution

  1. Step 1: Recall Spring AOP annotations

    @Before runs advice before the target method executes, perfect for logging method start.
  2. Step 2: Differentiate from other annotations

    @AfterReturning runs after method success, @Around wraps method, @AfterThrowing runs on exceptions.
  3. Final Answer:

    @Before -> Option D
  4. Quick Check:

    Log before method = @Before [OK]
Hint: Use @Before to log method start [OK]
Common Mistakes:
  • Confusing @Before with @AfterReturning
  • Using @AfterThrowing for normal logging
  • Thinking @Around is only for exceptions
3. Given this Spring AOP advice snippet, what will be logged when processOrder() method completes successfully?
@Before("execution(* com.example.service.*.*(..))")
public void logStart() {
    System.out.println("Method started");
}

@AfterReturning("execution(* com.example.service.*.*(..))")
public void logEnd() {
    System.out.println("Method completed");
}
medium
A. Method started and Method completed
B. Method completed only
C. Method started only
D. No output logged

Solution

  1. Step 1: Analyze @Before advice

    Before processOrder() runs, "Method started" is printed.
  2. Step 2: Analyze @AfterReturning advice

    After successful completion, "Method completed" is printed.
  3. Final Answer:

    Method started and Method completed -> Option A
  4. Quick Check:

    Before + AfterReturning logs both messages [OK]
Hint: Both @Before and @AfterReturning run on success [OK]
Common Mistakes:
  • Thinking only one advice runs
  • Confusing @AfterReturning with @AfterThrowing
  • Assuming no logs if method returns void
4. Identify the error in this Spring AOP logging advice code:
@Before("execution(* com.example..*(..))")
public void logStart() {
    System.out.println("Starting method");
}

@AfterReturning("execution(* com.example.service.*.*(..))")
public void logEnd() {
    System.out.println("Method ended");
}
medium
A. The pointcut expressions target different packages
B. The method signatures lack return types
C. The @AfterReturning advice is missing a returning attribute
D. The pointcut expression in @Before is invalid

Solution

  1. Step 1: Check pointcut expressions

    @Before targets "com.example..*" (all subpackages), @AfterReturning targets only "com.example.service.*".
  2. Step 2: Understand mismatch impact

    This mismatch means @Before runs on more methods than @AfterReturning, causing inconsistent logging.
  3. Final Answer:

    The pointcut expressions target different packages -> Option A
  4. Quick Check:

    Pointcut mismatch = A [OK]
Hint: Match pointcut packages exactly for consistent logging [OK]
Common Mistakes:
  • Assuming pointcut syntax is wrong
  • Thinking return type is required in advice methods
  • Believing @AfterReturning must have returning attribute always
5. You want to log method entry, exit, and exceptions for all service methods in package com.example.service. Which combination of Spring AOP advice annotations should you use?
hard
A. @AfterThrowing only
B. @Before, @AfterReturning, and @AfterThrowing
C. @Around only
D. @Before and @AfterReturning only

Solution

  1. Step 1: Identify logging needs

    Entry logging requires @Before, exit logging requires @AfterReturning, exception logging requires @AfterThrowing.
  2. Step 2: Confirm combined advice usage

    Using all three annotations covers all cases: start, successful end, and exceptions.
  3. Final Answer:

    @Before, @AfterReturning, and @AfterThrowing -> Option B
  4. Quick Check:

    All logging cases need these three advices [OK]
Hint: Use all three advices for full logging coverage [OK]
Common Mistakes:
  • Using only @Before and @AfterReturning misses exceptions
  • Thinking @Around alone is simpler for all cases
  • Ignoring exception logging with @AfterThrowing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard