{"record":{"id":"4325d293f5b00989","repo":"spring-projects/spring-framework","slug":"specify-either-targetobject-or-targetbeanname","errorCode":null,"errorMessage":"Specify either 'targetObject' or 'targetBeanName', not both","messagePattern":"Specify either 'targetObject' or 'targetBeanName', not both","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/config/PropertyPathFactoryBean.java","lineNumber":164,"sourceCode":"\t/**\n\t * The bean name of this PropertyPathFactoryBean will be interpreted\n\t * as \"beanName.property\" pattern, if neither \"targetObject\" nor\n\t * \"targetBeanName\" nor \"propertyPath\" have been specified.\n\t * This allows for concise bean definitions with just an id/name.\n\t */\n\t@Override\n\tpublic void setBeanName(String beanName) {\n\t\tthis.beanName = StringUtils.trimAllWhitespace(BeanFactoryUtils.originalBeanName(beanName));\n\t}\n\n\n\t@Override\n\tpublic void setBeanFactory(BeanFactory beanFactory) {\n\t\tthis.beanFactory = beanFactory;\n\t\tString targetBeanName = this.targetBeanName;\n\n\t\tif (this.targetBeanWrapper != null && targetBeanName != null) {\n\t\t\tthrow new IllegalArgumentException(\"Specify either 'targetObject' or 'targetBeanName', not both\");\n\t\t}\n\n\t\tif (this.targetBeanWrapper == null && targetBeanName == null) {\n\t\t\tif (this.propertyPath != null) {\n\t\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\t\"Specify 'targetObject' or 'targetBeanName' in combination with 'propertyPath'\");\n\t\t\t}\n\n\t\t\t// No other properties specified: check bean name.\n\t\t\tint dotIndex;\n\t\t\tif (this.beanName == null || (dotIndex = this.beanName.indexOf('.')) <= 0) {\n\t\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\t\"Neither 'targetObject' nor 'targetBeanName' specified, and PropertyPathFactoryBean \" +\n\t\t\t\t\t\t\"bean name '\" + this.beanName + \"' does not follow 'beanName.property' syntax\");\n\t\t\t}\n\t\t\ttargetBeanName = this.beanName.substring(0, dotIndex);\n\t\t\tthis.targetBeanName = targetBeanName;\n\t\t\tthis.propertyPath = this.beanName.substring(dotIndex + 1);","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/config/PropertyPathFactoryBean.java#L146-L182","documentation":"Thrown by PropertyPathFactoryBean.setBeanFactory() when both targetObject (an inline/instance target) and targetBeanName (a named target) have been configured. PropertyPathFactoryBean evaluates a property path on exactly one target; specifying both is ambiguous, so Spring rejects it at the BeanFactory-aware stage.","triggerScenarios":"XML with both <property name=\"targetObject\" ref=\"...\"> and <property name=\"targetBeanName\" value=\"...\"> on the same PropertyPathFactoryBean. Programmatic calls to setTargetObject(...) and setTargetBeanName(...).","commonSituations":"Copy-paste from two examples merging both properties. Refactoring from inline bean to bean-name reference and forgetting to remove the old property. Confusion about which form to use.","solutions":["Keep only one: use targetBeanName for a named bean reference, or targetObject for an inline/inner bean.","Prefer targetBeanName with a singleton target so Spring can eagerly resolve and determine the result type.","In code, delete whichever setTargetObject/setTargetBeanName call is unwanted."],"exampleFix":"<!-- before (broken): both set -->\n<bean class=\"...PropertyPathFactoryBean\">\n  <property name=\"targetObject\" ref=\"tb\"/>\n  <property name=\"targetBeanName\" value=\"tb\"/>\n  <property name=\"propertyPath\" value=\"age\"/>\n</bean>\n<!-- after -->\n<bean class=\"...PropertyPathFactoryBean\">\n  <property name=\"targetBeanName\" value=\"tb\"/>\n  <property name=\"propertyPath\" value=\"age\"/>\n</bean>","handlingStrategy":"validation","validationCode":"if (fb.getTargetObject() != null && /* targetBeanName set */ targetBeanName != null) {\n    throw new IllegalStateException(\"Choose targetObject OR targetBeanName, not both\");\n}","typeGuard":"static boolean hasSingleTarget(PropertyPathFactoryBean fb, String targetBeanName) {\n    return (fb.getTargetObject() == null) ^ (targetBeanName == null);\n}","tryCatchPattern":"try {\n    fb.setBeanFactory(beanFactory);\n} catch (IllegalArgumentException ex) {\n    if (ex.getMessage().contains(\"not both\")) {\n        // drop one of the two targets and retry\n    }\n    throw ex;\n}","preventionTips":["Decide on one style (bean-name ref vs inline object) and document it for the team.","Review PropertyPathFactoryBean definitions in code review for duplicate target properties.","Prefer targetBeanName for named singletons so Spring can resolve the result type eagerly."],"tags":["spring","factory-bean","property-path","configuration","ambiguous-config"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}