Bird
Raised Fist0
Spring Bootframework~20 mins

Cross-cutting concerns concept in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cross-cutting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Cross-cutting Concerns in Spring Boot
Which of the following best describes a cross-cutting concern in a Spring Boot application?
AA UI component that handles user input.
BA feature that affects multiple parts of the application, like logging or security, implemented separately from business logic.
CA database entity that stores user information.
DA specific business rule applied only in one service class.
Attempts:
2 left
💡 Hint
Think about features that are needed across many parts of the app, not just one place.
❓ component_behavior
intermediate
2:00remaining
Behavior of Aspect in Spring Boot
Given a Spring Boot aspect that logs method execution time, what will happen when a method annotated with @LogExecutionTime is called?
Spring Boot
@Aspect
@Component
public class LoggingAspect {
    @Around("@annotation(com.example.LogExecutionTime)")
    public Object logTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long executionTime = System.currentTimeMillis() - start;
        System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
        return proceed;
    }
}
AThe method runs twice because of the aspect.
BThe method is skipped and only the execution time is printed.
CThe method throws a runtime exception due to missing annotation.
DThe method runs normally and its execution time is printed to the console.
Attempts:
2 left
💡 Hint
The aspect wraps the method to measure time but does not prevent execution.
📝 Syntax
advanced
2:00remaining
Correct Syntax for Defining a Pointcut
Which option correctly defines a pointcut that matches all methods in the package com.example.service and its subpackages?
A@Pointcut("execution(* com.example.service..*(..))")
B@Pointcut("execution(* com.example.service.*(..))")
C@Pointcut("execution(* com.example..service.*(..))")
D@Pointcut("execution(* com.example.service.*.*(..))")
Attempts:
2 left
💡 Hint
Use '..' to include subpackages in pointcut expressions.
🔧 Debug
advanced
2:00remaining
Identifying the Cause of Aspect Not Triggering
A developer created an aspect to log method calls but notices it never runs. Which of the following is the most likely cause?
AThe aspect class is not annotated with @Component or not scanned by Spring.
BThe application is missing a database connection.
CThe method being called is private.
DThe method has no parameters.
Attempts:
2 left
💡 Hint
Spring needs to know about the aspect bean to apply it.
❓ state_output
expert
3:00remaining
Output of Logging Aspect with Multiple Method Calls
Given the following Spring Boot aspect and service, what will be printed to the console when main() calls service.process() twice?
Spring Boot
public class Service {
    @LogExecutionTime
    public void process() throws InterruptedException {
        Thread.sleep(100);
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Service service = new Service();
        service.process();
        service.process();
    }
}

@Aspect
@Component
public class LoggingAspect {
    @Around("@annotation(com.example.LogExecutionTime)")
    public Object logTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long time = System.currentTimeMillis() - start;
        System.out.println(joinPoint.getSignature() + " executed in " + time + "ms");
        return proceed;
    }
}
ACompilation error due to missing annotations on Service class.
BTwo lines printed showing execution time for each process() call.
CNo output because the aspect is not applied to the service instance created with new.
DOne line printed showing execution time for the first call only.
Attempts:
2 left
💡 Hint
Spring AOP works only on Spring-managed beans, not on objects created with new.

Practice

(1/5)
1. What is the main purpose of cross-cutting concerns in a Spring Boot application?
easy
A. To manage database connections and queries
B. To define the main business logic of the application
C. To create user interface components
D. To handle common tasks like logging and security across multiple parts of the app

Solution

  1. Step 1: Understand the role of cross-cutting concerns

    Cross-cutting concerns are tasks that affect many parts of an application, such as logging, security, or transaction management.
  2. Step 2: Identify their purpose in Spring Boot

    Spring Boot uses cross-cutting concerns to keep these common tasks separate from business logic, improving code clarity and reuse.
  3. Final Answer:

    To handle common tasks like logging and security across multiple parts of the app -> Option D
  4. Quick Check:

    Cross-cutting concerns = common tasks across app [OK]
Hint: Cross-cutting concerns handle shared tasks across many parts [OK]
Common Mistakes:
  • Confusing cross-cutting concerns with main business logic
  • Thinking they manage database or UI directly
  • Assuming they are only for security
2. Which annotation is used in Spring Boot to define an aspect for cross-cutting concerns?
easy
A. @Aspect
B. @Controller
C. @Service
D. @Repository

Solution

  1. Step 1: Recall Spring AOP annotations

    Spring Boot uses Aspect-Oriented Programming (AOP) to implement cross-cutting concerns, and the key annotation to define an aspect is @Aspect.
  2. Step 2: Differentiate from other annotations

    @Controller is for web controllers, @Service for service layer, and @Repository for data access. None of these define aspects.
  3. Final Answer:

    @Aspect -> Option A
  4. Quick Check:

    Aspect annotation = @Aspect [OK]
Hint: Use @Aspect to mark cross-cutting concern classes [OK]
Common Mistakes:
  • Choosing @Controller or @Service by mistake
  • Confusing @Aspect with @Component
  • Not knowing which annotation defines an aspect
3. Given this Spring Boot aspect code snippet:
@Aspect
public class LoggingAspect {
  @Before("execution(* com.example.service.*.*(..))")
  public void logBefore() {
    System.out.println("Method called");
  }
}

What will happen when a method in com.example.service package is called?
medium
A. The message "Method called" will print after the method executes
B. The message "Method called" will print before the method executes
C. No message will print because the pointcut is incorrect
D. The method call will be blocked and not executed

Solution

  1. Step 1: Understand the @Before advice

    The @Before annotation means the method logBefore() runs before the matched method executes.
  2. Step 2: Analyze the pointcut expression

    The pointcut execution(* com.example.service.*.*(..)) matches any method in any class inside com.example.service package.
  3. Final Answer:

    The message "Method called" will print before the method executes -> Option B
  4. Quick Check:

    @Before advice prints message before method [OK]
Hint: @Before runs code before matched methods [OK]
Common Mistakes:
  • Confusing @Before with @After advice
  • Thinking pointcut matches other packages
  • Assuming method is blocked
4. Identify the error in this Spring Boot aspect code:
@Aspect
public class SecurityAspect {
  @Before("execution(* com.example..*(..))")
  public void checkSecurity() {
    // security check code
  }
}
medium
A. The pointcut expression syntax is invalid
B. The method name must start with 'validate'
C. The advice method must return a value
D. The class is missing @Component or @Configuration annotation

Solution

  1. Step 1: Examine the pointcut expression

    The pointcut execution(* com.example..*(..)) has invalid syntax because it is missing the method name pattern .*. Correct syntax is execution(* com.example..*.*(..)) to match any method in com.example package and subpackages.
  2. Step 2: Rule out other options

    Advice methods can be void and have any name; missing bean annotation is secondary.
  3. Final Answer:

    The pointcut expression syntax is invalid -> Option A
  4. Quick Check:

    Pointcut requires .* for method pattern [OK]
Hint: Pointcut syntax: execution(* pkg..*.*(..)) [OK]
Common Mistakes:
  • Thinking it's missing @Component annotation
  • Believing method name must start with 'validate'
  • Assuming advice must return a value
5. You want to apply a logging aspect only to methods annotated with @Transactional in your Spring Boot app. Which pointcut expression correctly targets these methods?
hard
A. execution(@Transactional * *(..))
B. within(@Transactional *)
C. @annotation(org.springframework.transaction.annotation.Transactional)
D. args(@Transactional)

Solution

  1. Step 1: Understand pointcut for annotations

    To target methods with a specific annotation, Spring AOP uses the @annotation pointcut designator with the full annotation class name.
  2. Step 2: Evaluate options

    @annotation(org.springframework.transaction.annotation.Transactional) uses @annotation(org.springframework.transaction.annotation.Transactional), which correctly matches methods annotated with @Transactional. Other options are invalid or incorrect syntax.
  3. Final Answer:

    @annotation(org.springframework.transaction.annotation.Transactional) -> Option C
  4. Quick Check:

    Use @annotation for method-level annotation matching [OK]
Hint: Use @annotation with full annotation class for method targeting [OK]
Common Mistakes:
  • Using execution with annotation incorrectly
  • Confusing within and args pointcuts
  • Not specifying full annotation package
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