skip to content

bean() Spring-Specific Designator

bean() matches by Spring bean name with wildcards and exists only in Spring AOP, with no AspectJ equivalent. A quick way to scope advice to a naming convention, and a good detail to have when asked about Spring-specific extensions.

part ofSpring Frameworkoverview, primer and where to startread it →
on this pageshow

questions

5

What does the bean() pointcut designator do in Spring AOP, and how do you use it?

level: juniorimportance: must knowfreq 55%

answer

  1. matches by Spring bean NAME not type
  2. wildcard: bean(*Service)
  3. Spring-AOP only, no AspectJ equivalent
  4. often AND-ed with execution()
  5. FactoryBean: matches the produced bean

basics

~10 s

bean(name) limits advice to join points inside Spring beans whose bean name matches the given pattern. For example, bean(tradeService) advises only the bean named tradeService, no matter its type.

solid answer

~40 s

bean(idOrNamePattern) is a pointcut designator that matches join points located within Spring beans identified by name. You give it a bean id/name, optionally with a * wildcard, e.g. bean(tradeService) or bean(*Service). It scopes advice by bean name rather than by class, method signature, or annotation. It is commonly combined with execution(...) to say "any method in beans named *Service". Because it works on Spring's bean-name registry, it is a Spring-AOP-only designator with no AspectJ equivalent. You use it in @Pointcut / @Before / @Around expressions just like execution() or within(): @Before("bean(*Service)") advises every advised method in beans whose name ends with Service.

code

java · 14 lines
java
@Aspect
@Component
public class ServiceMonitoringAspect {

    // Advise every advised method on any bean whose name ends in "Service"
    @Before("bean(*Service)")
    public void logEntry(JoinPoint jp) {
        System.out.println("enter: " + jp.getSignature());
    }

    // Narrow to one specific bean by exact name
    @AfterReturning("bean(tradeService)")
    public void afterTrade() { /* ... */ }
}

go deeper

for a junior

Know it matches join points by bean NAME and supports the * wildcard.

for a middle

Know it combines with execution() via && and that it targets names, not types.

for a senior

Explain the Spring-only nature and naming-convention use cases vs type/annotation scoping.

for a principal

Discuss FactoryBean matching, name-vs-alias semantics, and governance trade-offs of name-based advice.

## What bean() is In Spring AOP a **pointcut** is a predicate that selects **join points** (in Spring, always method executions on Spring beans). Spring supports the standard AspectJ pointcut designators (`execution`, `within`, `@annotation`, `args`, `target`, `this`, etc.) **plus one designator of its own**: `bean(idOrNamePattern)`. `bean(...)` matches join points that occur **within a Spring bean identified by its bean name** (the id/name in the `ApplicationContext`, e.g. the default `tradeService` derived from a `@Service` class `TradeService`, or an explicit `@Bean`/XML name). ## Syntax ``` bean(idOrNamePattern) ``` - The argument is a **bean name**, not a fully-qualified type. - A single `*` wildcard is supported inside the name: `bean(*Service)`, `bean(user*)`, `bean(*Repository)`. - It can be combined with the boolean operators `&&`, `||`, `!`: `bean(*Service) || bean(*Dao)`, `!bean(internal*)`. ## How you use it It appears anywhere a pointcut expression is expected — in an `@Aspect` class: ```java @Aspect @Component class AuditAspect { @Before("bean(*Service)") void audit(JoinPoint jp) { /* runs before every advised method of beans named *Service */ } } ``` Most often it is **AND-ed with a signature designator** to narrow both the bean and the method: ```java @Around("execution(* *(..)) && bean(orderService)") ``` ## What it matches on It matches on the **Spring bean name**, decided at proxy-creation time. That means: - Two beans of the **same class** but different names can be advised differently. - Renaming the bean (changing its id) changes what the pointcut selects, even though the code is unchanged. - For a `FactoryBean`, the match applies to the **exposed bean** produced by the factory (the object you get from `getBean(name)`), not to the `&name` factory object itself. ## Why it exists / when to use it It is the idiomatic way to scope advice **by naming convention** rather than by package, type, or annotation — e.g. "apply this transaction/monitoring advice to every bean whose name ends in `Service`." It is especially handy when you cannot annotate the target classes and their **types don't share a common package or supertype**, but they **do** follow a name convention. ## Key limitation `bean()` is a **Spring-AOP-only** designator. AspectJ has no notion of Spring bean names, so if you use `bean(...)` in a pointcut that is meant to be woven by the AspectJ weaver (LTW/CTW native AspectJ aspects), it is **not understood**. It only works within Spring's proxy-based AOP.

  • Does bean(*Service) match by class name or bean name?
    By bean name (the id in the ApplicationContext). For a @Service TradeService the default bean name is tradeService, so bean(*Service) matches it because the NAME ends in 'Service' — but if you registered it under a different name it would not match, even though the class name still ends in Service.
  • Can you use bean() with pure AspectJ load-time weaving?
    No. bean() is a Spring-AOP-only designator; the AspectJ weaver has no concept of Spring bean names, so it is not recognized outside Spring's proxy-based AOP.

context

open as a page

How do wildcards and boolean operators work with bean(), and how would you scope advice to services but exclude a few beans?

level: middleimportance: should knowfreq 38%

basics

~10 s

bean() supports a * wildcard in the name, e.g. bean(*Service), and combines with &&, ||, ! like other pointcuts. To advise services except some beans: bean(*Service) && !bean(legacyService).

open as a page

Why is bean() described as a Spring-AOP-only designator with no AspectJ equivalent, and what are the consequences?

level: seniorimportance: should knowfreq 40%

basics

~20 s

AspectJ weaves at the class/bytecode level and knows nothing about Spring bean names, so it has no bean() designator. bean() only works inside Spring's proxy-based AOP, where the container maps join points to named beans.

open as a page

When would you choose bean() over within(), @within, or a custom annotation for scoping advice?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Use bean() when the beans you want to advise share a naming convention but not a package, type, or annotation — and you can't or don't want to modify their classes. Use within/@within/@annotation when selection should follow type or annotation instead.

open as a page

What subtle behaviors of bean() around bean naming, aliases, FactoryBeans, and infrastructure beans should you know?

level: principalimportance: nice to knowfreq 16%

basics

~20 s

bean() matches the container's registered bean name. For a FactoryBean it matches the produced object's name (not the &name factory), it can't advise infrastructure/non-proxied beans, and it matches names not aliases in a predictable way — so know exactly what name a bean carries.

open as a page