{"record":{"id":"c47b7e918997ec72","repo":"spring-projects/spring-framework","slug":"methodinvocation-is-not-a-spring-proxymethodinvoca-c47b7e","errorCode":null,"errorMessage":"MethodInvocation is not a Spring ProxyMethodInvocation: {mi}","messagePattern":"MethodInvocation is not a Spring ProxyMethodInvocation: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisors.java","lineNumber":72,"sourceCode":"\t * has been included in the interceptor chain, and that the invocation is exposed\n\t * with ExposeInvocationInterceptor.\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() 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/**","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisors.java#L54-L90","documentation":"Thrown by ExposeBeanNameAdvisors.getBeanName(MethodInvocation) (line 71-73) when the passed MethodInvocation is not an instance of Spring's ProxyMethodInvocation. The bean name is stored as a user attribute on the ProxyMethodInvocation; a generic AOP Alliance MethodInvocation has no user-attribute API, so the lookup cannot proceed and an IllegalArgumentException is raised.","triggerScenarios":"Calling ExposeBeanNameAdvisors.getBeanName(mi) where 'mi' is a hand-rolled or third-party MethodInvocation implementation that does not implement org.springframework.aop.ProxyMethodInvocation. This includes unit tests passing a mocked MethodInvocation that is not typed as ProxyMethodInvocation.","commonSituations":"Unit-testing advice that reads the bean name by passing a plain mock MethodInvocation. Using a non-Spring AOP framework's invocation inside a Spring-advised chain. Calling getBeanName from a thread that received a raw MethodInvocation.","solutions":["Ensure the MethodInvocation you pass is a Spring ProxyMethodInvocation (e.g. ReflectiveMethodInvocation, or the one Spring supplies during proxy invocation).","In tests, pass a mock of ProxyMethodInvocation and stub getUserAttribute/setUserAttribute.","Only invoke getBeanName(mi) from within a Spring-managed proxy chain, not from arbitrary invocation objects."],"exampleFix":"// before\nMethodInvocation mi = mock(MethodInvocation.class);\nString name = ExposeBeanNameAdvisors.getBeanName(mi); // fails\n\n// after\nProxyMethodInvocation mi = mock(ProxyMethodInvocation.class);\nwhen(mi.getUserAttribute(anyString())).thenReturn(\"myBean\");\nString name = ExposeBeanNameAdvisors.getBeanName(mi);","handlingStrategy":"type-guard","validationCode":"import org.springframework.aop.ProxyMethodInvocation;\nif (!(mi instanceof ProxyMethodInvocation)) {\n    throw new IllegalArgumentException(\"Need a Spring ProxyMethodInvocation\");\n}","typeGuard":"static boolean isSpringProxyInvocation(MethodInvocation mi) {\n    return mi instanceof ProxyMethodInvocation;\n}","tryCatchPattern":null,"preventionTips":["Call ExposeBeanNameAdvisors.getBeanName only on Spring ProxyMethodInvocations.","In tests, mock ProxyMethodInvocation, not the bare interface.","Run bean-name lookups inside Spring-managed proxy chains."],"tags":["spring-aop","bean-name","proxy","reflection"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}