{"id":"fc4449da866c0bf1","repo":"spring-projects/spring-framework","slug":"cannot-get-bean-name-not-set-on-methodinvocation","errorCode":null,"errorMessage":"Cannot get bean name; not set on MethodInvocation: {mi}","messagePattern":"Cannot get bean name; not set on MethodInvocation: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisors.java","lineNumber":76,"sourceCode":"\t */\n\tpublic static String getBeanName() throws IllegalStateException {\n\t\treturn getBeanName(ExposeInvocationInterceptor.currentInvocation());\n\t}\n\n\t/**\n\t * Find the bean name for the given invocation. Assumes that an ExposeBeanNameAdvisor\n\t * has been included in the interceptor chain.\n\t * @param mi the MethodInvocation that should contain the bean name as an attribute\n\t * @return the bean name (never {@code null})\n\t * @throws IllegalStateException if the bean name has not been exposed\n\t */\n\tpublic static String getBeanName(MethodInvocation mi) throws IllegalStateException {\n\t\tif (!(mi instanceof ProxyMethodInvocation pmi)) {\n\t\t\tthrow new IllegalArgumentException(\"MethodInvocation is not a Spring ProxyMethodInvocation: \" + mi);\n\t\t}\n\t\tString beanName = (String) pmi.getUserAttribute(BEAN_NAME_ATTRIBUTE);\n\t\tif (beanName == null) {\n\t\t\tthrow new IllegalStateException(\"Cannot get bean name; not set on MethodInvocation: \" + mi);\n\t\t}\n\t\treturn beanName;\n\t}\n\n\t/**\n\t * Create a new advisor that will expose the given bean name,\n\t * with no introduction.\n\t * @param beanName bean name to expose\n\t */\n\tpublic static Advisor createAdvisorWithoutIntroduction(String beanName) {\n\t\treturn new DefaultPointcutAdvisor(new ExposeBeanNameInterceptor(beanName));\n\t}\n\n\t/**\n\t * Create a new advisor that will expose the given bean name, introducing\n\t * the NamedBean interface to make the bean name accessible without forcing\n\t * the target object to be aware of this Spring IoC concept.\n\t * @param beanName the bean name to expose","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisors.java#L58-L94","documentation":"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.","triggerScenarios":"Calling ExposeBeanNameAdvisors.getBeanName(mi) on an invocation whose interceptor chain did not include createAdvisorWithoutIntroduction(beanName) or createAdvisorIntroducingNamedBean(beanName).","commonSituations":"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.","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."],"exampleFix":"// before\nProxyFactory pf = new ProxyFactory(target);\npf.addAdvice(someAdvice);\n// no expose-bean-name advisor -> getBeanName() fails\n\n// after\nProxyFactory pf = new ProxyFactory(target);\npf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction(\"myBean\"));\npf.addAdvice(someAdvice);","handlingStrategy":"try-catch","validationCode":"boolean beanNameExposed(org.springframework.aop.ProxyMethodInvocation pmi) {\n    return pmi.getUserAttribute(ExposeBeanNameAdvisors.class.getName() + \".BEAN_NAME\") != null;\n}","typeGuard":null,"tryCatchPattern":"try {\n    return ExposeBeanNameAdvisors.getBeanName(mi);\n} catch (IllegalStateException ex) {\n    // advisor not in chain; degrade gracefully\n    return null;\n}","preventionTips":["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."],"tags":["spring-aop","bean-name","configuration","advisor-chain"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}