0
0
Spring Bootframework~5 mins

@Aspect annotation in Spring Boot

Choose your learning style9 modes available
Introduction

The @Aspect annotation marks a class as an aspect, which helps add extra behavior to your code without changing its core logic.

You want to log method calls across many classes without repeating code.
You need to check security permissions before running certain methods.
You want to measure how long methods take to run for performance checks.
You want to handle errors in a consistent way across multiple methods.
Syntax
Spring Boot
@Aspect
public class MyAspect {
    // advice methods here
}
Use @Aspect on a class to tell Spring it contains advice (extra code to run).
This class usually works with @Before, @After, or @Around annotations to define when the extra code runs.
Examples
This aspect logs a message before any method in com.example.service package runs.
Spring Boot
@Aspect
public class LoggingAspect {
    @Before("execution(* com.example.service.*.*(..))")
    public void logBefore() {
        System.out.println("Method is about to run");
    }
}
This aspect measures and prints how long any method in com.example package takes to run.
Spring Boot
@Aspect
public class TimingAspect {
    @Around("execution(* com.example..*(..))")
    public Object measureTime(org.aspectj.lang.ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        System.out.println("Execution took " + (end - start) + " ms");
        return result;
    }
}
Sample Program

This example shows a simple aspect that prints a message before the sayHello method runs in MyService. When you run the app and call sayHello, you see the aspect's message first, then the service's message.

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 SimpleLoggingAspect {

    @Before("execution(* com.example.demo.MyService.sayHello(..))")
    public void beforeSayHello() {
        System.out.println("About to call sayHello method");
    }
}

// Service class
package com.example.demo;

import org.springframework.stereotype.Service;

@Service
public class MyService {
    public void sayHello() {
        System.out.println("Hello from MyService!");
    }
}

// Main application
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        MyService service = context.getBean(MyService.class);
        service.sayHello();
    }
}
OutputSuccess
Important Notes

Remember to add @Component to your aspect class so Spring can find it.

Use pointcut expressions like execution(* package.Class.method(..)) to select where advice runs.

Aspects help keep your code clean by separating extra tasks like logging or security.

Summary

@Aspect marks a class as an aspect to add extra behavior.

Use it with advice annotations like @Before to run code at specific times.

It helps keep your main code simple and clean by handling cross-cutting concerns separately.