spring-projects/spring-framework · error · IllegalStateException

Cannot get bean name; not set on MethodInvocation

Error message

Cannot get bean name; not set on MethodInvocation: {mi}

What it means

Thrown by ExposeBeanNameAdvisors.getBeanName(MethodInvocation) (line 74-77) when the invocation is a valid ProxyMethodInvocation but the BEAN_NAME_ATTRIBUTE user attribute is null. That attribute is only populated by an ExposeBeanNameInterceptor/ExposeBeanNameIntroduction present in the interceptor chain; if no such advisor was added, the attribute is never set and an IllegalStateException is raised.

Solutions

  1. Add an expose-bean-name advisor to the advice chain: pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction(beanName)).
  2. If using auto-proxying, ensure the advisor is registered as a bean so it participates in the chain.
  3. Verify you are calling getBeanName on an invocation produced by a proxy that actually contains the expose-bean-name advice.

Example fix

// before
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(myAdvice); // no expose-bean-name advisor
// myAdvice calls ExposeBeanNameAdvisors.getBeanName(mi) -> fails

// after
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction("myBean"));
pf.addAdvice(myAdvice);
Defensive patterns

Strategy: validation

Validate before calling

String name = (String)
    ((ProxyMethodInvocation) mi).getUserAttribute(
        ExposeBeanNameAdvisors.class.getName() + ".BEAN_NAME");
if (name == null) {
    // ensure the expose-bean-name advisor is in the chain
}

Prevention

When it happens

Trigger: Calling getBeanName(mi) on a Spring proxy invocation whose interceptor chain did not include an advisor created via ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction(...) or createAdvisorIntroducingNamedBean(...). The invocation is a ProxyMethodInvocation but the bean name was never attached.

Common situations: Forgetting to add the expose-bean-name advisor to the proxy factory / advisor chain. Reading the bean name from an advice that runs on a different proxy where the bean-name advisor is absent. Mixing manual ProxyFactory usage that omits the expose advisor.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/fc4449da866c0bf1. Report an issue: GitHub.

Appendix: 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 expose

View on GitHub (pinned to 69bf83ad71)