Bird
Raised Fist0
Spring Bootframework~20 mins

AOP for logging in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
AOP Logging Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding AOP Pointcut Expressions

Which pointcut expression correctly matches all methods in classes within the package com.example.service and its subpackages?

Awithin(com.example.service.*)
Bexecution(* com.example.service.*(..))
Cexecution(* com.example.service..*(..))
Dexecution(* com.example.service..*.*(..))
Attempts:
2 left
💡 Hint

Remember that .. matches subpackages and any number of arguments.

💻 Command Output
intermediate
2:00remaining
Output of Logging Aspect Around Advice

Given the following Spring AOP around advice, what will be printed when the method processOrder() is called?

Spring Boot
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println("Start: " + joinPoint.getSignature().getName());
    Object result = joinPoint.proceed();
    System.out.println("End: " + joinPoint.getSignature().getName());
    return result;
}
AStart: processOrder\nEnd: processOrder
BStart: processOrder
CEnd: processOrder\nStart: processOrder
DEnd: processOrder
Attempts:
2 left
💡 Hint

Think about the order of statements before and after proceed().

❓ Configuration
advanced
2:00remaining
Correct Aspect Configuration for Logging

Which of the following Spring configuration snippets correctly enables AspectJ auto-proxying and declares a logging aspect bean?

A@Configuration\n@ComponentScan(basePackages = "com.example")\npublic class AppConfig {\n}
B@Configuration\n@EnableAspectJAutoProxy\npublic class AppConfig {\n @Bean\n public LoggingAspect loggingAspect() {\n return new LoggingAspect();\n }\n}
C@EnableAspectJAutoProxy\n@Component\npublic class LoggingAspect {}
D@Configuration\n@EnableTransactionManagement\npublic class AppConfig {\n @Bean\n public LoggingAspect loggingAspect() {\n return new LoggingAspect();\n }\n}
Attempts:
2 left
💡 Hint

Enabling AspectJ auto-proxying is required for aspects to work.

❓ Troubleshoot
advanced
2:00remaining
Diagnosing Missing Logs from Aspect

A developer created a logging aspect with @Around advice but no logs appear when methods run. What is the most likely cause?

AThe aspect class is not annotated with <code>@Aspect</code>
BThe methods being called are private
CThe logging statements use <code>System.out.println</code> instead of a logger
DThe application is missing <code>spring-boot-starter-web</code> dependency
Attempts:
2 left
💡 Hint

Check if the aspect is recognized by Spring AOP.

✅ Best Practice
expert
2:00remaining
Choosing the Best Advice Type for Logging Execution Time

Which advice type is best suited to measure and log the execution time of a method in Spring AOP?

A@Before advice, because it runs before the method starts
B@AfterReturning advice, because it runs after method returns successfully
C@AfterThrowing advice, because it runs if the method throws an exception
D@Around advice, because it can run code before and after method execution
Attempts:
2 left
💡 Hint

Think about needing to capture time before and after the method runs.

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