What is an Advisor in Spring AOP, and how do PointcutAdvisor, DefaultPointcutAdvisor, and NameMatchMethodPointcut fit together?
answer
- Advisor = advice + where
- PointcutAdvisor = advice + Pointcut
- DefaultPointcutAdvisor(pointcut, advice)
- Pointcut = ClassFilter + MethodMatcher
- NameMatchMethodPointcut = match by name, '*' wildcard, ignores params
basics
~20 sAn Advisor bundles an advice with the information about where to apply it. A PointcutAdvisor pairs an advice with a Pointcut. DefaultPointcutAdvisor is the standard implementation; NameMatchMethodPointcut is a Pointcut that matches methods by name.
solid answer
~40 sIn Spring's classic API, advice alone says 'what to do' but not 'where'. An Advisor (org.springframework.aop.Advisor) is the pairing of an advice with the metadata for applying it. The common subtype PointcutAdvisor adds a Pointcut answering 'which classes and methods'. DefaultPointcutAdvisor is the ready-made implementation: you construct it with a Pointcut and an Advice. A Pointcut has two parts — a ClassFilter and a MethodMatcher. NameMatchMethodPointcut is a convenient Pointcut whose ClassFilter matches all classes and whose MethodMatcher matches by method name (via setMappedName/setMappedNames, with '*' wildcards). You wire it as: new DefaultPointcutAdvisor(pointcut, advice), then hand the advisor to a ProxyFactory/ProxyFactoryBean or let a DefaultAdvisorAutoProxyCreator pick it up. This decouples the advice from targeting, so the same advice can be reused with different pointcuts.
code
java · 19 linesimport org.springframework.aop.Advisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.aop.framework.ProxyFactory;
public class WiringExample {
public static MyService buildProxy(MyService target) {
NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
pointcut.setMappedNames("save", "update", "delete*"); // '*' wildcard supported
Advisor advisor = new DefaultPointcutAdvisor(pointcut, new TimingInterceptor());
ProxyFactory factory = new ProxyFactory(target);
factory.addAdvisor(advisor);
return (MyService) factory.getProxy();
}
}
// Or register the Advisor as a @Bean and add a DefaultAdvisorAutoProxyCreator
// bean so Spring auto-proxies matching beans without manual ProxyFactory code.go deeper
Know Advisor pairs advice with where-to-apply and NameMatchMethodPointcut matches by method name.
Wire DefaultPointcutAdvisor(pointcut, advice) into a ProxyFactory and explain Pointcut = ClassFilter + MethodMatcher.
Explain auto-proxy creation via DefaultAdvisorAutoProxyCreator, ordering, and NameMatchMethodPointcut's name-only limitation.
Position advisors as the substrate under @AspectJ, discuss IntroductionAdvisor vs PointcutAdvisor and static vs dynamic MethodMatcher performance.
### Why Advisors exist An **advice** (`MethodInterceptor`, `MethodBeforeAdvice`, etc.) only expresses **what** cross-cutting behavior to run. It says nothing about **where** to apply it. Spring introduces the **Advisor** to bind an advice to targeting metadata — it is Spring's classic-API unit of 'aspect = advice + where'. ### Advisor (`org.springframework.aop.Advisor`) The base interface. Its key method is `getAdvice()`. There are two families: - **PointcutAdvisor** — advice applied at **method join points** selected by a **Pointcut**. - **IntroductionAdvisor** — advice that adds new interfaces to the target (introductions); uses only a `ClassFilter`, not a method pointcut. ### PointcutAdvisor (`org.springframework.aop.PointcutAdvisor`) Adds `getPointcut()`. So a `PointcutAdvisor` = one `Advice` + one `Pointcut`. This is by far the most common Advisor type. ### DefaultPointcutAdvisor (`org.springframework.aop.support.DefaultPointcutAdvisor`) The generic, ready-to-use implementation. You typically do: ```java Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice); ``` If you set no pointcut, it defaults to `Pointcut.TRUE` (matches everything) — effectively applying the advice to every method. ### Pointcut = ClassFilter + MethodMatcher `org.springframework.aop.Pointcut` exposes: - `getClassFilter()` — which **classes** are eligible. - `getMethodMatcher()` — which **methods** match (with an optional runtime/dynamic check on arguments). ### NameMatchMethodPointcut (`org.springframework.aop.support.NameMatchMethodPointcut`) A `StaticMethodMatcherPointcut` whose `ClassFilter` is `TRUE` (all classes) and whose method matching is **by name**: - `setMappedName("save")` — a single method name. - `setMappedNames("save", "update", "delete*")` — several, supporting the `*` wildcard (`xxx*`, `*xxx`, `*xxx*`). - `addMethodName("...")` — programmatic addition. It matches by **name only**, ignoring parameter types — so all overloads of a matched name are advised. For richer expressions (return type, annotations, argument types) you'd use `AspectJExpressionPointcut` instead. ### Putting it together (the wiring) ```java NameMatchMethodPointcut pc = new NameMatchMethodPointcut(); pc.setMappedNames("save", "update"); Advisor advisor = new DefaultPointcutAdvisor(pc, new TimingInterceptor()); ProxyFactory pf = new ProxyFactory(realService); pf.addAdvisor(advisor); MyService proxied = (MyService) pf.getProxy(); ``` Alternatively, register the advisor as a bean and add a **`DefaultAdvisorAutoProxyCreator`**: it scans the context for all `Advisor` beans and automatically proxies any bean whose methods match — no explicit `ProxyFactory` wiring needed. ### Ordering & selection - An Advisor may implement `org.springframework.core.Ordered` to control chain position when several apply. - With auto-proxy creators, all matching advisors are collected, ordered, and composed into one interceptor chain per bean. ### Gotchas - **Advice without an Advisor** given to an auto-proxy creator won't be targeted by a pointcut — the creator works with `Advisor` beans (though `ProxyFactory.addAdvice` wraps a bare advice in a `DefaultPointcutAdvisor(TRUE)` for you). - `NameMatchMethodPointcut` ignoring parameter types can over-match overloaded methods. - Matching is against the **method as seen on the target/proxied type**; self-invocation still bypasses the proxy. ### When to use The Advisor + Pointcut + advice triangle is the low-level foundation that `@AspectJ` and `<aop:config>` build on. Use it directly for programmatic proxying, framework code, or when you need fine-grained, code-driven pointcut logic; prefer `@AspectJ` pointcut expressions for ordinary application aspects.
- What two components make up a Spring Pointcut, and what does each decide?A ClassFilter (which classes/types are eligible) and a MethodMatcher (which methods match, with an optional dynamic per-invocation check on arguments). Both must match for a join point to be advised.
- How can a DefaultPointcutAdvisor be applied to beans automatically?Register it (and the advice) as beans and add a DefaultAdvisorAutoProxyCreator. It scans for all Advisor beans and proxies any bean whose methods satisfy their pointcuts, ordering advisors via Ordered.
- What is a limitation of NameMatchMethodPointcut versus AspectJExpressionPointcut?It matches only by method name (with '*' wildcards) and ignores parameter types, return types, and annotations, so it advises all overloads of a name. AspectJExpressionPointcut supports full AspectJ pointcut expressions for precise selection.
saying these in an interview costs you the question
- Saying an Advisor is the same thing as an advice (it also carries the pointcut/targeting)
- Thinking DefaultPointcutAdvisor requires a MethodInterceptor specifically (any Advice works)
- Claiming NameMatchMethodPointcut can filter by argument or return types
- Believing a Pointcut has only a MethodMatcher and no ClassFilter