spring-projects/spring-framework · error · IllegalStateException
Cannot get bean name; not set on MethodInvocation: {mi}
Error message
Cannot get bean name; not set on MethodInvocation: {mi} What it means
getBeanName throws IllegalStateException when the invocation is a ProxyMethodInvocation but the BEAN_NAME_ATTRIBUTE user attribute is null, meaning no ExposeBeanNameInterceptor / ExposeBeanNameIntroduction ran in the chain for this invocation. The advisor that would have set the attribute was absent or its pointcut did not match.
Source
Thrown at spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisors.java:76
*/
public static String getBeanName() throws IllegalStateException {
return getBeanName(ExposeInvocationInterceptor.currentInvocation());
}
/**
* Find the bean name for the given invocation. Assumes that an ExposeBeanNameAdvisor
* has been included in the interceptor chain.
* @param mi the MethodInvocation that should contain the bean name as an attribute
* @return the bean name (never {@code null})
* @throws IllegalStateException if the bean name has not been exposed
*/
public static String getBeanName(MethodInvocation mi) throws IllegalStateException {
if (!(mi instanceof ProxyMethodInvocation pmi)) {
throw new IllegalArgumentException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
}
String beanName = (String) pmi.getUserAttribute(BEAN_NAME_ATTRIBUTE);
if (beanName == null) {
throw new IllegalStateException("Cannot get bean name; not set on MethodInvocation: " + mi);
}
return beanName;
}
/**
* Create a new advisor that will expose the given bean name,
* with no introduction.
* @param beanName bean name to expose
*/
public static Advisor createAdvisorWithoutIntroduction(String beanName) {
return new DefaultPointcutAdvisor(new ExposeBeanNameInterceptor(beanName));
}
/**
* Create a new advisor that will expose the given bean name, introducing
* the NamedBean interface to make the bean name accessible without forcing
* the target object to be aware of this Spring IoC concept.
* @param beanName the bean name to exposeView on GitHub (pinned to e8729d0438)
Solutions
- Add ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction(beanName) (or the introducing variant) to the proxy's advisor chain.
- Verify the advisor's pointcut actually matches the invoked method.
- Only call getBeanName from within an advised invocation that the advisor precedes.
Example fix
// before
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(someAdvice);
// no expose-bean-name advisor -> getBeanName() fails
// after
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction("myBean"));
pf.addAdvice(someAdvice); Defensive patterns
Strategy: try-catch
Validate before calling
boolean beanNameExposed(org.springframework.aop.ProxyMethodInvocation pmi) {
return pmi.getUserAttribute(ExposeBeanNameAdvisors.class.getName() + ".BEAN_NAME") != null;
} Try / catch
try {
return ExposeBeanNameAdvisors.getBeanName(mi);
} catch (IllegalStateException ex) {
// advisor not in chain; degrade gracefully
return null;
} Prevention
- Register createAdvisorWithoutIntroduction/createAdvisorIntroducingNamedBean in the chain.
- Verify the advisor pointcut matches before relying on getBeanName.
- Unit-test the full proxy chain to confirm the attribute is set.
When it happens
Trigger: Calling ExposeBeanNameAdvisors.getBeanName(mi) on an invocation whose interceptor chain did not include createAdvisorWithoutIntroduction(beanName) or createAdvisorIntroducingNamedBean(beanName).
Common situations: Forgetting to register the expose-bean-name advisor; the advisor's pointcut filtered out the method; calling getBeanName on a proxy that was created before the advisor was added.
Related errors
- MethodInvocation is not a Spring ProxyMethodInvocation: {}
- 'argumentNames' property of AbstractAspectJAdvice contains a
- Only afterReturning advice can be used to bind a return valu
- Only afterThrowing advice can be used to bind a thrown excep
- Advice method [{}] requires {} arguments to be bound by name
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/fc4449da866c0bf1.json.
Report an issue: GitHub.