{"id":"1c25e48f1486e173","repo":"spring-projects/spring-framework","slug":"property-targetbeanname-is-required","errorCode":null,"errorMessage":"Property 'targetBeanName' is required","messagePattern":"Property 'targetBeanName' is required","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/config/MethodLocatingFactoryBean.java","lineNumber":65,"sourceCode":"\t * @param targetBeanName the name of the bean to locate the {@link Method} on\n\t */\n\tpublic void setTargetBeanName(String targetBeanName) {\n\t\tthis.targetBeanName = targetBeanName;\n\t}\n\n\t/**\n\t * Set the name of the {@link Method} to locate.\n\t * <p>This property is required.\n\t * @param methodName the name of the {@link Method} to locate\n\t */\n\tpublic void setMethodName(String methodName) {\n\t\tthis.methodName = methodName;\n\t}\n\n\t@Override\n\tpublic void setBeanFactory(BeanFactory beanFactory) {\n\t\tif (!StringUtils.hasText(this.targetBeanName)) {\n\t\t\tthrow new IllegalArgumentException(\"Property 'targetBeanName' is required\");\n\t\t}\n\t\tif (!StringUtils.hasText(this.methodName)) {\n\t\t\tthrow new IllegalArgumentException(\"Property 'methodName' is required\");\n\t\t}\n\n\t\tClass<?> beanClass = beanFactory.getType(this.targetBeanName);\n\t\tif (beanClass == null) {\n\t\t\tthrow new IllegalArgumentException(\"Can't determine type of bean with name '\" + this.targetBeanName + \"'\");\n\t\t}\n\t\tthis.method = BeanUtils.resolveSignature(this.methodName, beanClass);\n\n\t\tif (this.method == null) {\n\t\t\tthrow new IllegalArgumentException(\"Unable to locate method [\" + this.methodName +\n\t\t\t\t\t\"] on bean [\" + this.targetBeanName + \"]\");\n\t\t}\n\t}\n\n","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/config/MethodLocatingFactoryBean.java#L47-L83","documentation":"Thrown by MethodLocatingFactoryBean.setBeanFactory() when the 'targetBeanName' property has not been set (blank or null). MethodLocatingFactoryBean is an internal FactoryBean used by the <aop:config> parser to locate an advice method on an aspect bean; it needs both the bean name and the method name to resolve the Method reflectively. A blank targetBeanName means the parser could not determine which aspect bean owns the advice method.","triggerScenarios":"Spring internally builds a MethodLocatingFactoryBean bean definition while parsing <aop:before method=\"...\"/> inside <aop:aspect>. The targetBeanName is derived from the <aspect ref=\"...\"> attribute; if the aspect ref is blank/missing in a context where an advice node is present, or the bean definition is constructed incorrectly, targetBeanName stays blank and setBeanFactory throws.","commonSituations":"An <aop:aspect> element declares advice (<aop:before> etc.) but omits or leaves blank the 'ref' attribute — normally the parser pre-empts this with its own error, but edge cases (custom namespace extension, programmatic bean definition construction) can reach this guard. Programmatic misuse of MethodLocatingFactoryBean without calling setTargetBeanName.","solutions":["In XML, ensure each <aop:aspect> that declares advice has a non-empty 'ref' attribute pointing to the aspect bean.","When constructing MethodLocatingFactoryBean programmatically, always call setTargetBeanName(beanName) before the bean is initialized.","Validate the <aop:config> block against the Spring AOP schema to catch missing attributes early."],"exampleFix":"<!-- before -->\n<aop:config>\n  <aop:aspect id=\"auditAspect\">\n    <aop:before method=\"log\" pointcut=\"execution(* *(..))\"/>\n  </aop:aspect>\n</aop:config>\n<!-- after -->\n<aop:config>\n  <aop:aspect id=\"auditAspect\" ref=\"auditBean\">\n    <aop:before method=\"log\" pointcut=\"execution(* *(..))\"/>\n  </aop:aspect>\n</aop:config>","handlingStrategy":"validation","validationCode":"import org.w3c.dom.Element;\n\nvoid ensureAspectRefSet(Element aspectElement) {\n  String ref = aspectElement.getAttribute(\"ref\");\n  if (ref == null || ref.isBlank())\n    throw new IllegalArgumentException(\"<aop:aspect> declaring advice must set a non-empty 'ref' attribute\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set ref=\"aspectBean\" on <aop:aspect> elements that contain advice.","Prefer @AspectJ annotations to remove a class of XML attribute-omission bugs.","Validate <aop:config> files against the Spring AOP schema during CI."],"tags":["spring-aop","xml-config","factory-bean","configuration"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}