{"record":{"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/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisors.java#L58-L94","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add an expose-bean-name advisor to the advice chain: pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction(beanName)).","If using auto-proxying, ensure the advisor is registered as a bean so it participates in the chain.","Verify you are calling getBeanName on an invocation produced by a proxy that actually contains the expose-bean-name advice."],"exampleFix":"// before\nProxyFactory pf = new ProxyFactory(target);\npf.addAdvice(myAdvice); // no expose-bean-name advisor\n// myAdvice calls ExposeBeanNameAdvisors.getBeanName(mi) -> fails\n\n// after\nProxyFactory pf = new ProxyFactory(target);\npf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction(\"myBean\"));\npf.addAdvice(myAdvice);","handlingStrategy":"validation","validationCode":"String name = (String)\n    ((ProxyMethodInvocation) mi).getUserAttribute(\n        ExposeBeanNameAdvisors.class.getName() + \".BEAN_NAME\");\nif (name == null) {\n    // ensure the expose-bean-name advisor is in the chain\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always add the expose-bean-name advisor to the proxy that needs getBeanName.","Verify the advisor bean is registered when using auto-proxying.","Read the bean name only within the same chain that sets it."],"tags":["spring-aop","bean-name","advisor","configuration"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}