{"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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/interceptor/ExposeBeanNameAdvisors.java#L54-L90","documentation":"ExposeBeanNameAdvisors.getBeanName(MethodInvocation) throws IllegalArgumentException when the supplied invocation is not a Spring ProxyMethodInvocation. The bean-name mechanism stores the name as a user attribute via pmi.setUserAttribute, an API only ProxyMethodInvocation exposes; a generic AOP Alliance MethodInvocation cannot carry it.","triggerScenarios":"Calling ExposeBeanNameAdvisors.getBeanName(mi) where mi is a hand-rolled or third-party AOP Alliance MethodInvocation that does not implement org.springframework.aop.ProxyMethodInvocation.","commonSituations":"Interoperating Spring AOP with another AOP Alliance implementation; unit tests passing a mocked MethodInvocation; mixing in non-Spring proxies.","solutions":["Ensure the invocation originates from a Spring-generated proxy (ProxyFactory, ProxyFactoryBean, or a Spring bean proxy).","If you must call getBeanName, obtain the invocation via ExposeInvocationInterceptor.currentInvocation() which Spring populates.","Do not pass custom MethodInvocation implementations into ExposeBeanNameAdvisors."],"exampleFix":"// before\nMethodInvocation mi = myCustomAopAllianceInvocation;\nString name = ExposeBeanNameAdvisors.getBeanName(mi); // not a ProxyMethodInvocation\n\n// after\nMethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();\nString name = ExposeBeanNameAdvisors.getBeanName(mi);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"boolean isSpringInvocation(org.aopalliance.intercept.MethodInvocation mi) {\n    return mi instanceof org.springframework.aop.ProxyMethodInvocation;\n}","tryCatchPattern":"try {\n    String name = ExposeBeanNameAdvisors.getBeanName(mi);\n} catch (IllegalArgumentException ex) {\n    // invocation is not a Spring ProxyMethodInvocation; fall back to another strategy\n}","preventionTips":["Only call ExposeBeanNameAdvisors on invocations from Spring proxies.","Acquire the current invocation via ExposeInvocationInterceptor.currentInvocation().","Avoid mixing third-party AOP Alliance MethodInvocation types."],"tags":["spring-aop","proxy","bean-name","method-invocation"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}