Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you create an aspect in Spring?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedOct 17, 2024
UpdatedNov 16, 2026
5 min read

You create an aspect in Spring by writing a class annotated with @Aspect and @Component, enabling AspectJ-style proxying (automatic in Spring Boot, or @EnableAspectJAutoProxy otherwise), and adding advice methods tied to pointcut expressions. Spring then wraps matching beans in proxies that run your advice around their method calls.

An aspect is the right place for cross-cutting concerns such as logging, timing, auditing and security checks that would otherwise be repeated across many classes. This guide builds a working logging and timing aspect for Spring Boot 3.

Table of Contents

  • What an aspect is and when to use one
  • Adding the dependency and enabling AOP
  • Writing the aspect class
  • Applying the aspect to a service
  • Targeting methods with annotations
  • Limits and troubleshooting
  • Frequently Asked Questions
    • Is @Component required on an aspect?
    • Do I need AspectJ to use Spring AOP?
    • Can an aspect apply to private methods?
    • How do I control the order of several aspects?
  • Summary

What an aspect is and when to use one

An aspect packages behaviour that cuts across modules. Without it, every service method repeats the same logging or authorization code. With Spring AOP you write the behaviour once and select the target methods with a pointcut.

ConceptRole
AspectThe class holding the cross-cutting logic
AdviceThe code that runs, for example before or around a method
PointcutThe expression selecting which methods are affected
Join pointA method execution that a pointcut can match

Spring AOP is proxy based and supports method execution on Spring beans only. For constructors, fields or non-Spring objects, use full AspectJ weaving.

Adding the dependency and enabling AOP

In Spring Boot, add the starter and Boot configures auto-proxying for you:

xml

In a plain Spring application, enable it explicitly and include aspectjweaver on the classpath:

java

Writing the aspect class

The aspect below logs every call to the service package and measures its duration.

java

Two annotations are required: @Aspect marks the class as an aspect, and @Component (or a @Bean definition) registers it in the container. Forgetting @Component is the most common reason an aspect does nothing.

Applying the aspect to a service

java

When code calls greetingService.greet("Asha"), the output looks like this:

text

The caller receives a proxy that runs the advice and then delegates to the real bean. The ordering between different advice types is not something to depend on.

Targeting methods with annotations

Package-based pointcuts break when you rename packages. A custom annotation makes the intent explicit:

java
java

Now only methods marked @Timed are measured. Use @Order on the aspect class when several aspects apply to the same method; lower values run first on the way in.

Limits and troubleshooting

  • Self-invocation: calling this.other() inside the same bean skips the proxy, so no advice runs.
  • Only public methods of Spring beans are advised with the default proxy setup. private and final methods cannot be intercepted.
  • The aspect class itself cannot be advised by another aspect.
  • If an advice is not firing, check that the class is a bean, the pointcut matches the method, and spring-boot-starter-aop is on the classpath.
  • Do not put heavy logic in @Around advice for hot paths; each call adds proxy overhead.

Frequently Asked Questions

Is @Component required on an aspect?

Yes, or an equivalent bean registration. @Aspect only describes the class; Spring must manage it to apply it.

Do I need AspectJ to use Spring AOP?

You use AspectJ's annotations and pointcut language through aspectjweaver, but you do not need the AspectJ compiler or weaver for Spring AOP.

Can an aspect apply to private methods?

No. Spring AOP intercepts public method calls through proxies. Use AspectJ weaving for other join points.

How do I control the order of several aspects?

Add @Order(1), @Order(2) and so on to the aspect classes, or implement Ordered. The lowest value has the highest precedence.

Summary

To create an aspect in Spring, write a class with @Aspect and @Component, define pointcuts, and add advice methods such as @Before, @AfterReturning and @Around. Spring Boot enables proxying with the AOP starter. Prefer annotation-based pointcuts and remember that self-invocation is not advised.

Share