0
0
Spring Bootframework~5 mins

@Before advice in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Before advice in Spring AOP?

The @Before advice runs a method before the target method executes. It is used to perform actions like logging, security checks, or setting up resources before the main method runs.

Click to reveal answer
beginner
How do you declare a @Before advice in Spring Boot?

You create a method annotated with @Before and provide a pointcut expression that matches the methods where you want the advice to run.

@Before("execution(* com.example.service.*.*(..))")
public void beforeAdvice() {
  // code to run before target method
}
Click to reveal answer
intermediate
What is a pointcut expression in the context of @Before advice?

A pointcut expression defines which methods the advice should apply to. It uses patterns to match method names, packages, or classes.

Click to reveal answer
intermediate
Can @Before advice modify the return value of the target method?

No, @Before advice runs before the method and cannot change the return value. To modify return values, use @Around advice.

Click to reveal answer
beginner
Give a real-life example where @Before advice is useful.

Before a bank transaction method runs, @Before advice can check if the user is authenticated and log the transaction attempt.

Click to reveal answer
What does @Before advice do in Spring AOP?
AModifies the return value of the target method
BRuns code after the target method executes
CRuns code before the target method executes
DHandles exceptions thrown by the target method
Which annotation is used to declare a method as a @Before advice?
A@Before
B@After
C@Around
D@Pointcut
Can @Before advice stop the execution of the target method?
AYes, by throwing an exception
BNo, it always lets the method run
CYes, by returning false
DNo, it runs after the method
What is a pointcut expression used for in @Before advice?
ATo define when the advice runs
BTo define which methods the advice applies to
CTo modify method arguments
DTo log method return values
Which advice type allows modifying the return value of a method?
A@Before
B@AfterThrowing
C@After
D@Around
Explain how @Before advice works in Spring Boot and give an example use case.
Think about what you want to do before a method runs.
You got /3 concepts.
    Describe the difference between @Before advice and @Around advice.
    Consider when each advice runs and what it can do.
    You got /3 concepts.