0
0
Spring Bootframework~5 mins

Cross-cutting concerns concept in Spring Boot

Choose your learning style9 modes available
Introduction

Cross-cutting concerns are parts of a program that affect many areas, like logging or security. They help keep code clean by handling these common tasks separately.

When you want to log actions happening in many parts of your app without repeating code.
When you need to check user permissions before running different functions.
When you want to measure how long methods take to run across your app.
When you want to handle errors in a consistent way everywhere.
When you want to add caching to improve performance without changing business logic.
Syntax
Spring Boot
@Aspect
@Component
public class MyAspect {

    @Before("execution(* com.example.service.*.*(..))")
    public void beforeMethod() {
        // code to run before methods
    }

    @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result")
    public void afterMethod(Object result) {
        // code to run after methods
    }
}
Use @Aspect to mark a class as handling cross-cutting concerns.
Use pointcut expressions like execution(* package.Class.method(..)) to select where to apply the concern.
Examples
This runs logging code before any method in the controller package.
Spring Boot
@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.controller.*.*(..))")
    public void logBefore() {
        System.out.println("Method is about to run");
    }
}
This checks security before service methods run.
Spring Boot
@Aspect
@Component
public class SecurityAspect {

    @Before("execution(* com.example.service.*.*(..))")
    public void checkSecurity() {
        // check user permissions here
    }
}
Sample Program

This Spring Boot app uses an aspect to log a message before any method in MyService runs. When doWork() runs, the aspect prints a log first, then the method prints its 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.*(..))")
    public void logBeforeMethod() {
        System.out.println("[LOG] A method in MyService is starting");
    }
}

package com.example.demo;

import org.springframework.stereotype.Service;

@Service
public class MyService {
    public void doWork() {
        System.out.println("Doing work...");
    }
}

package com.example.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    private final MyService myService;

    public DemoApplication(MyService myService) {
        this.myService = myService;
    }

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

    @Override
    public void run(String... args) {
        myService.doWork();
    }
}
OutputSuccess
Important Notes

Cross-cutting concerns help keep your main code simple and focused.

Spring Boot uses AspectJ annotations to handle these concerns easily.

Be careful with pointcut expressions to avoid applying aspects where not needed.

Summary

Cross-cutting concerns handle tasks like logging and security across many parts of an app.

Spring Boot uses @Aspect and pointcuts to apply these concerns cleanly.

This keeps your main code easier to read and maintain.