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;
@Aspect
@Order(1)
@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:config>
<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" />
</aop:aspect>
</aop:config>
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;
@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());
context.close();
}
}
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:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.1.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.20</version>
</dependency>
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; @Configuration @EnableAspectJAutoProxy @ComponentScan 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 !!
Comments