Spring AOP AspectJ @Before Annotation Example

In this Spring AOP example, we will learn to use AspectJ @Before annotation. The @Before annotation is applied to a method within an aspect that should be executed before the advised method is invoked. This is specially useful in logging, cleanup tasks or any other post-execution operations without tightly coupling them to the business logic.

In this tutorial, we will create a simple spring application, add a logging aspect, and then invoke aspect methods based on pointcuts information passed in @Before annotation.

1. AspectJ @Before Annotation Usage

We can use @Before annotation in manner below. In the following example, the logBeforeAllMethods() method is annotated with @Before, indicating that it should be executed before all the methods matching the specified pointcut expression.

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

public class LoggingAspect {

  private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);

  @Before("execution(* com.howtodoinjava.core.aop.service.EmployeeService.*(..))")
  public void logBeforeAllMethods(JoinPoint joinPoint) {
    logger.info("LoggingAspect: Logging before aspect execution");

The equivalent XML configuration is:

<!-- Enable AspectJ auto proxy -->
<aop:aspectj-autoproxy />

<!-- Define the aspect -->
<bean id="loggingAspect" class="com.example.aspect.LoggingAspect" />

<!-- Configure the aspect and pointcut -->
    <aop:aspect id="beforeAspect" ref="loggingAspect">
        <aop:pointcut id="beforePointcut"
                      expression="execution(* com.howtodoinjava.core.aop.service.EmployeeService.*(..))" />
        <aop:before method="logBeforeAllMethods" pointcut-ref="beforePointcut" />

2. Demo

When we run any method in the class com.howtodoinjava.core.aop.service.EmployeeService then we can see that the logBeforeAllMethods() is executed before it.

import com.howtodoinjava.core.demo.beans.Employee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

public class EmployeeService {

  private static final Logger logger = LoggerFactory.getLogger(EmployeeService.class);

  public Employee save(Employee employee) {
    logger.info("Saving the Employee...");
    return null;

Let’s run a method and verify the output.

import com.howtodoinjava.core.aop.service.EmployeeService;
import com.howtodoinjava.core.demo.beans.Employee;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

    EmployeeService employeeService = context.getBean(EmployeeService.class);
    employeeService.save(new Employee());


The program output is:

2023-11-27T12:25:13.092+0530 INFO LoggingAspect: Logging before aspect execution
2023-11-27T12:25:13.093+0530 INFO Saving the Employee...

3. Maven

To get the @Before annotation working, we need to ensure that Spring AOP is setup correctly. So make sure we have the following dependencies:




4. Enabling AOP

Also, we must enable the AOP support to @Aspect annotation to work.

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

public class AppConfig {

5. Conclusion

In this Spring AOP tutorial, we explored using the AspectJ @Before annotation that allows us to inject the required behavior before the execution of advised methods, enhancing modularity and maintainability.

Happy Learning !!

Soure Code on Github


Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode