0
0
Spring Bootframework~5 mins

@Before advice in Spring Boot

Choose your learning style9 modes available
Introduction

@Before advice lets you run some code right before a specific method runs. It helps you add extra steps like checking or logging without changing the main method.

You want to log a message every time a method starts.
You need to check user permissions before running a method.
You want to validate input before the main method runs.
You want to measure how long a method takes by starting a timer before it runs.
Syntax
Spring Boot
@Before("pointcutExpression")
public void adviceMethod() {
    // code to run before target method
}
The pointcutExpression defines which methods the advice applies to.
The advice method runs before the matched method but cannot change its arguments or result.
Examples
This runs before any method in the com.example.service package.
Spring Boot
@Before("execution(* com.example.service.*.*(..))")
public void logBefore() {
    System.out.println("Method is about to run");
}
This runs only before the createUser method in UserService.
Spring Boot
@Before("execution(public void com.example.service.UserService.createUser(..))")
public void checkBeforeCreate() {
    System.out.println("Checking before creating user");
}
Sample Program

This Spring Boot app uses @Before advice to print a log message before the sayHello method runs. When you run the app, it calls sayHello which triggers the advice first.

Spring Boot
package com.example.demo;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.demo.GreetingService.sayHello(..))")
    public void beforeSayHello() {
        System.out.println("[LOG] sayHello method is about to run");
    }
}

package com.example.demo;

import org.springframework.stereotype.Service;

@Service
public class GreetingService {
    public void sayHello(String name) {
        System.out.println("Hello, " + name + "!");
    }
}

package com.example.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication
@EnableAspectJAutoProxy
public class DemoApplication implements CommandLineRunner {

    private final GreetingService greetingService;

    public DemoApplication(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) {
        greetingService.sayHello("Alice");
    }
}
OutputSuccess
Important Notes

Make sure to enable AspectJ auto proxy with @EnableAspectJAutoProxy in your Spring Boot app.

@Before advice cannot stop or change the method it runs before; it only adds extra steps.

Summary

@Before advice runs code right before a matched method starts.

It is useful for logging, checking, or preparing before the main method.

You define which methods to target using pointcut expressions.