Quick revision
The short answer
within(type pattern) matches every method execution inside types whose name matches the pattern, while @within(Annotation) matches method executions inside types that declare the given annotation. @annotation(Annotation) matches only methods that themselves carry the annotation. this(Type) matches when the proxy object is an instance of Type, and target(Type) when the target, the real bean behind the proxy, is; with JDK proxies the two differ, because the proxy implements only the interfaces and is not an instance of the target's class.
What to remember
In this answer, we compared five designators. within matches methods declared in types matching a name pattern, and @within matches methods in types that carry an annotation. @annotation matches only methods that carry the annotation themselves. this matches on the type of the proxy and target on the type of the real bean, and with JDK proxies they differ, because the proxy implements only interfaces. Grouping them by the question each answers makes the right choice easy.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
within(type pattern) matches every method execution inside types whose name matches the pattern, while @within(Annotation) matches method executions inside types that declare the given annotation. @annotation(Annotation) matches only methods that themselves carry the annotation. this(Type) matches when the proxy object is an instance of Type, and target(Type) when the target, the real bean behind the proxy, is; with JDK proxies the two differ, because the proxy implements only the interfaces and is not an instance of the target's class.
Why this question is asked
These five designators look similar and are easy to mix up, and choosing the wrong one gives an aspect that either never runs or runs far more often than intended. Interviewers ask about them because the difference between this and target in particular shows whether we understand what a Spring proxy really is. In this answer, we will recap what a designator is, look at the two type-based designators and the two annotation-based ones with examples of what each matches, then work through this and target with both kinds of proxy, and finish with a comparison table.
1A Quick Recap of Designators
A pointcut expression selects method executions for an advice. Each expression starts with a designator, a keyword that says what to match on. execution matches on the method signature. The five in this question match on other things: the type the method belongs to, annotations on that type or method, and the runtime type of the proxy or the target. In Spring AOP, the only join point is method execution on a bean, so every one of these designators ultimately selects method executions; they just use different properties to decide.
Two terms matter here. The target is our real bean object. The proxy is the object Spring generates around it, either a JDK dynamic proxy implementing the bean's interfaces or a CGLIB subclass of the bean's class.
2Within and @within: Matching by the Type
within(type pattern) matches method executions whose code is declared in a type matching the pattern. It takes the same type patterns as execution, including wildcards and .. for sub-packages. @within(Annotation) matches method executions in types whose declaration carries that annotation.
So within is about names and packages, and @within is about a marker on the class. @within is the usual way to say all methods of classes marked with our annotation, for example @within(com.acme.Monitored). Notice that both look at the declaring type: a method that a class inherits from an unannotated superclass is declared in the superclass, so @within on the subclass does not match it. There is also @target(Annotation), which checks the runtime class of the target object instead of the declaring type; it is similar but evaluated at runtime, so it is more expensive and in Spring Boot can try to proxy unexpected beans, so @within is usually preferred.
3@annotation: Matching by the Method
@annotation(Annotation) matches a method execution when the executing method has the annotation. It is the most precise of the annotation designators, and it is how Spring's own features such as @Transactional, @Cacheable and @Async conceptually select their methods.
The difference from @within is just where the annotation sits: on the method or on the class. Combining both, as in the last line, gives the familiar behaviour of annotations like @Transactional that can be placed at either level.
4This and target: The Proxy Versus the Real Bean
this(Type) matches when the currently executing object, which in Spring AOP is the proxy, is an instance of Type. target(Type) matches when the target object, the bean the proxy forwards to, is an instance of Type. With a CGLIB proxy, the proxy is a subclass of the bean's class, so it is an instance of everything the target is, and the two designators usually agree. With a JDK dynamic proxy, the proxy implements only the bean's interfaces, so it is not an instance of the implementation class, and the two give different answers.
| Expression | JDK proxy | CGLIB proxy |
|---|---|---|
this(OrderService) | matches | matches |
this(OrderServiceImpl) | no match | matches |
target(OrderService) | matches | matches |
target(OrderServiceImpl) | matches | matches |
this() with the concrete class under a JDK proxy.So target is the safer choice when we mean the kind of bean this is, because it does not depend on how Spring proxied it. this is useful when we really care about the proxy's type, for example to match beans whose proxies implement an introduced interface. Both forms also have a binding variant, such as target(service) with an OrderServiceImpl parameter in the advice, which gives the advice the object itself.
5Putting the Five Side by Side
It helps to group them by question. Which type contains the method, by name? within. Which type contains the method, by annotation? @within. Is this particular method annotated? @annotation. Is the object being called, the proxy, of this type? this. Is the real bean of this type? target. With that grouping, choosing becomes straightforward: for an opt-in aspect, @annotation or @within; for a layer-wide rule, within on a package; and for type-based rules, target, unless we deliberately want the proxy's type.
| Designator | Matches when | Typical use |
|---|---|---|
within(pattern) | method is declared in a type matching the pattern | all methods in a package or class |
@within(Ann) | declaring type has annotation Ann | all methods of marked classes |
@annotation(Ann) | executing method has annotation Ann | opt-in per method, e.g. @Audited |
this(Type) | the proxy is an instance of Type | matching on proxy or introduced types |
target(Type) | the target bean is an instance of Type | matching on the bean's real type |
6Follow-up questions
Why are this and target not the same in AspectJ either?
In AspectJ, this is the object currently executing and target is the object a call is made on. For a call join point they are the caller and the callee. Spring AOP only has execution join points, so it maps this to the proxy and target to the bean, which keeps the idea of two distinct objects.
Does within() match methods of subclasses?
within(com.acme.Base) matches only methods declared in Base. To include subclasses, use within(com.acme.Base+), where the plus sign means the type and all its subtypes.
Which is cheaper to evaluate, @within or @target?
@within can be decided statically from the class declaration when Spring builds the proxy. @target depends on the runtime type of the object, so Spring may need to check it at runtime and may create proxies for more beans than necessary. @within is the usual choice.
7Conclusion
In this answer, we compared five designators. within matches methods declared in types matching a name pattern, and @within matches methods in types that carry an annotation. @annotation matches only methods that carry the annotation themselves. this matches on the type of the proxy and target on the type of the real bean, and with JDK proxies they differ, because the proxy implements only interfaces. Grouping them by the question each answers makes the right choice easy.
Open with
within takes a type pattern and matches method executions whose code is declared in a matching type, so within(com.acme.service..*) means every method of every class in that package tree. @within takes an annotation and matches methods in classes whose declaration has that annotation, so @within(Service) means all methods of @Service classes. @annotation is narrower: it matches only when the executing method itself has the annotation, which is how you build opt-in aspects like @Audited, and if you want the @Transactional style of class or method level you combine @annotation or @within. this and target are about runtime types.
Then, if they want more
In Spring AOP, this is the proxy and target is the real bean behind it. With a CGLIB proxy, which is a subclass, both normally agree. With a JDK dynamic proxy, the proxy only implements the interfaces, so this(OrderServiceImpl) does not match while target(OrderServiceImpl) does.
So I use target when I mean the bean's real type, and this only when I deliberately care about the proxy's type, for example an introduced interface. I would also mention @target, which checks the runtime class rather than the declaring type and is more expensive, so @within is usually the better choice.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain the difference between the within, @within, @annotation, this and target...": https://scrivio-interview-library.netlify.app/spring-boot/aop/d76e982b0b/