skip to content

How do you declare an aspect with @Aspect and make Spring actually apply it? What steps are required?

level: juniorimportance: must knowfreq 80%

answer

  1. @Aspect marks, @Component registers
  2. @Aspect alone is NOT a bean
  3. @EnableAspectJAutoProxy (Boot auto)
  4. proxy-based, not weaving
  5. self-invocation bypasses advice

basics

~20 s

Put @Aspect on the class to mark it as an aspect, and also register it as a Spring bean (e.g. add @Component so it is scanned). Then enable AOP with @EnableAspectJAutoProxy (Spring Boot does this automatically).

solid answer

~40 s

Three things are needed. First, annotate the class with @Aspect (from AspectJ, org.aspectj.lang.annotation) to mark it as an aspect holding pointcuts and advice. Second, register it as a Spring bean — @Aspect alone does NOT make it a bean; typically add @Component so component-scan picks it up, or declare an @Bean. Third, enable aspect auto-proxying: @EnableAspectJAutoProxy on a @Configuration class (or the equivalent XML <aop:aspectj-autoproxy/>). Spring Boot enables this automatically via AopAutoConfiguration when AspectJ is on the classpath. Without the bean registration the aspect is invisible; without auto-proxying Spring never creates the proxies that invoke the advice. Note Spring uses @AspectJ annotations for configuration but its own proxy-based runtime, not full AspectJ weaving.

code

java · 18 lines
java
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect          // marks it as an aspect (AspectJ annotation)
@Component        // registers it as a Spring bean so Spring can find it
public class AuditAspect {

    @Before("execution(* com.example.service.*.*(..))")
    public void logEntry() {
        System.out.println("entering service method");
    }
}

// Somewhere in configuration (not needed in Spring Boot):
// @Configuration
// @EnableAspectJAutoProxy
// public class AopConfig {}

go deeper

for a junior

Know the two annotations: @Aspect to mark, @Component to register, and that Boot enables AOP for you.

for a middle

Explain the auto-proxy BeanPostProcessor and why @Aspect alone is invisible; know proxy vs weaving.

for a senior

Discuss JDK vs CGLIB proxy selection and the self-invocation limitation as a consequence of proxying.

for a principal

Contrast Spring AOP proxying with AspectJ LTW when constructor/field/self-call interception is required; reason about bean-post-processor ordering.

**What an aspect is.** In Aspect-Oriented Programming (AOP) a *cross-cutting concern* (logging, security checks, transactions, metrics) is code that would otherwise be scattered across many methods. An **aspect** is a module that captures such a concern in one place. It combines **pointcuts** (expressions selecting *where* to act — which method executions) with **advice** (the code to run — `@Before`, `@After`, `@Around`, `@AfterReturning`, `@AfterThrowing`). **@Aspect.** `@org.aspectj.lang.annotation.@Aspect` marks a class as an aspect. Spring reuses AspectJ's *annotation syntax* but runs it with **Spring AOP** (runtime proxies), not AspectJ's compile/load-time weaving. So you write `@Aspect`/`@Before` etc., but the interception is done by dynamic proxies. **Registering it as a bean — the key gotcha.** `@Aspect` by itself is just metadata; it does **not** turn the class into a Spring bean and Spring will not detect it. You must register the aspect as a bean by the normal means: - add a stereotype like `@Component` (or `@Service`) so `@ComponentScan` picks it up, **or** - declare it with an `@Bean` factory method in a `@Configuration` class, **or** - an XML `<bean>` definition. A very common beginner bug is annotating a class only with `@Aspect`, seeing nothing happen, and not realizing the bean was never created. **Enabling auto-proxying.** Spring only weaves aspects if aspect auto-proxying is turned on. You do this with `@EnableAspectJAutoProxy` on a `@Configuration` class, or `<aop:aspectj-autoproxy/>` in XML. This imports the `AnnotationAwareAspectJAutoProxyCreator`, a `BeanPostProcessor` that: (1) finds all `@Aspect` beans, (2) reads their pointcuts, and (3) wraps matching target beans in proxies (JDK dynamic proxies if the target implements interfaces, CGLIB subclass proxies otherwise). When a method on the proxy is called, the proxy consults the matching advice. **Spring Boot.** With `spring-boot-starter-aop` (or AspectJ on the classpath), `AopAutoConfiguration` enables `@EnableAspectJAutoProxy` for you, so you usually only need `@Aspect` + `@Component`. **Why proxies matter for behavior.** Because Spring AOP works through proxies, advice only fires on **external** calls that go through the proxy. A **self-invocation** (one method in a bean calling another `this.method()`) bypasses the proxy and therefore bypasses the advice — a classic surprise unrelated to how you declared the aspect. **Summary checklist:** `@Aspect` (mark) + bean registration (`@Component`/`@Bean`) + auto-proxying enabled (`@EnableAspectJAutoProxy` or Boot). Miss any one and the advice silently never runs.

  • You added @Aspect and @Component but the advice never runs. What are the likely causes?
    Auto-proxying may be off (missing @EnableAspectJAutoProxy and not a Boot app with the AOP starter); the pointcut may not match; the target bean may not be a Spring bean; or the call is a self-invocation on the same instance, which bypasses the proxy.
  • Does @Aspect on its own create a Spring bean?
    No. @Aspect only marks the class as an aspect. You must register it separately as a bean (via @Component, @Bean, or XML) or Spring will not instantiate it and its advice will never apply.

saying these in an interview costs you the question

  • Believing @Aspect automatically registers the class as a Spring bean
  • Thinking Spring AOP does full AspectJ bytecode weaving by default
  • Forgetting that auto-proxying must be enabled (assuming it is always on outside Boot)
  • Expecting advice to fire on internal self-invocation calls

context