Flat Preloader Icon

Spring AOP AspectJ Annotation

  • In Spring AOP, you can use AspectJ annotations to define aspects and apply cross-cutting concerns to your Spring beans. AspectJ is a powerful and widely-used AOP framework that Spring integrates seamlessly with.
  • AspectJ annotations provide a concise and expressive way to create aspects and specify where and how advice should be applied to your application’s code.
Here are some key AspectJ annotations used in Spring AOP:

@Aspect:

  • This annotation marks a class as an aspect. An aspect is a module that encapsulates a cross-cutting concern, such as logging or security. Aspect classes contain advice methods that define what to do at specific join points in the application.
				
					@Aspect
public class LoggingAspect {
    // Advice methods go here
}

				
			

@Before:

  • This annotation marks a method as “before” advice. The annotated method will be executed before a specified join point.
				
					@Before("execution(* com.example.service.*.*(..))")
public void logBefore(JoinPoint joinPoint) {
    // Advice logic before the method execution
}

				
			

@AfterReturning:

  • This annotation marks a method as “after returning” advice. The annotated method will be executed after a specified join point if it returns normally (without exceptions).
				
					@AfterReturning(pointcut = "execution
(* com.example.service.*.*(..))", returning = "result")
public void logAfterReturning
(JoinPoint joinPoint, Object result) {
    // Advice logic after a method returns successfully
}

				
			

@AfterThrowing:

  • This annotation marks a method as “after throwing” advice. The annotated method will be executed after a specified join point if it throws an exception.
				
					@AfterThrowing(pointcut = "execution
(* com.example.service.*.*(..))", throwing = "ex")
public void logAfterThrowing
(JoinPoint joinPoint, Exception ex) {
    // Advice logic after a method throws an exception
}

				
			

@Around:

  • This annotation marks a method as “around” advice. The annotated method has the most control over the join point. It can proceed to the join point’s execution, skip it, or modify its behavior.
				
					@Around("execution(* com.example.service.*.*(..))")
public Object logAround
(ProceedingJoinPoint joinPoint) throws Throwable {
    // Advice logic before and after the method execution
    Object result = joinPoint.proceed();
    // Advice logic after the method returns
    return result;
}

				
			

@Pointcut:

  • This annotation allows you to define a reusable pointcut expression. Pointcuts specify where advice should be applied. You can then reference these pointcuts in advice methods.
				
					@Pointcut("execution(* com.example.service.*.*(..))")
public void serviceMethods() {
}

@Before("serviceMethods()")
public void logBeforeServiceMethods() {
    // Advice logic before service methods
}

				
			

@DeclareParents:

  • This annotation introduces new interfaces and implementations to existing classes. It is used to achieve AspectJ’s “introduction” feature.
				
					@DeclareParents(value = "com.example.dao.*+",
defaultImpl = MyMixin.class)
public static MyMixinInterface mixin;

				
			
  • These are some of the key AspectJ annotations that you can use in Spring AOP. With these annotations, you can define aspects, advice, pointcuts, and more, making it easier to modularize cross-cutting concerns in your Spring applications.