Search code examples
springaopspring-aop

what is the difference between @within and @annotation in AOP


@within

This PCD limits matching to join points within types that have the given annotation:

@Pointcut("@within(org.springframework.stereotype.Repository)")

@annotation

This PCD limits matching to join points where the subject of the join point has the given annotation. For example we may create a @Loggable annotation:

@Pointcut("@annotation(org.baeldung.aop.annotations.Loggable)")
public void loggableMethods() {}

So does it mean that @annotation only applies for user defined or custom annotations. and @within for Standard Annotatios


Solution

  • This might be an older question, but just wanted to add the answer for more visibility.

    It is as JBNizet stated in the comment, the javadoc provides a clear difference between the two:

    @within: Limits matching to join points within types that have the given annotation (the execution of methods declared in types with the given annotation when using Spring AOP).

    @annotation: Limits matching to join points where the subject of the join point (the method being executed in Spring AOP) has the given annotation.

    from https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop-pointcuts-designators