{"record":{"id":"8ef9b3a143a3e092","repo":"spring-projects/spring-framework","slug":"specify-either-targetclass-or-targetobject-not-bo","errorCode":null,"errorMessage":"Specify either targetClass or targetObject, not both","messagePattern":"Specify either targetClass or targetObject, not both","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/config/FieldRetrievingFactoryBean.java","lineNumber":164,"sourceCode":"\t * nor \"targetField\" 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\t@Override\n\tpublic void setBeanClassLoader(ClassLoader classLoader) {\n\t\tthis.beanClassLoader = classLoader;\n\t}\n\n\n\t@Override\n\t@SuppressWarnings(\"NullAway\") // Dataflow analysis limitation\n\tpublic void afterPropertiesSet() throws ClassNotFoundException, NoSuchFieldException {\n\t\tif (this.targetClass != null && this.targetObject != null) {\n\t\t\tthrow new IllegalArgumentException(\"Specify either targetClass or targetObject, not both\");\n\t\t}\n\n\t\tif (this.targetClass == null && this.targetObject == null) {\n\t\t\tif (this.targetField != null) {\n\t\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\t\"Specify targetClass or targetObject in combination with targetField\");\n\t\t\t}\n\n\t\t\t// If no other property specified, consider bean name as static field expression.\n\t\t\tif (this.staticField == null) {\n\t\t\t\tthis.staticField = this.beanName;\n\t\t\t\tAssert.state(this.staticField != null, \"No target field specified\");\n\t\t\t}\n\n\t\t\t// Try to parse static field into class and field.\n\t\t\tint lastDotIndex = this.staticField.lastIndexOf('.');\n\t\t\tif (lastDotIndex == -1 || lastDotIndex == this.staticField.length()) {\n\t\t\t\tthrow new IllegalArgumentException(","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/config/FieldRetrievingFactoryBean.java#L146-L182","documentation":"Thrown as an IllegalArgumentException by FieldRetrievingFactoryBean.afterPropertiesSet when both targetClass and targetObject are set. The factory bean needs exactly one of them to know where to look up the static field; providing both is contradictory, so initialization refuses to proceed rather than guessing.","triggerScenarios":"Configuring FieldRetrievingFactoryBean with both setTargetClass(...) and setTargetObject(...) (or both <property name='targetClass'> and <property name='targetObject'> in XML). afterPropertiesSet checks both non-null and throws before any field lookup.","commonSituations":"XML bean definition that fills both targetClass and targetObject by mistake; copy-paste of a working config where one property should have been removed; programmatic GenericBeanDefinition setting both via fluent API; @Bean returning a FieldRetrievingFactoryBean with both set.","solutions":["Provide exactly one of targetClass (for a static field on a class) or targetObject (for an instance field), not both.","If you want a static field, keep targetClass + targetField and drop targetObject.","If you want an instance field, keep targetObject + targetField and drop targetClass.","Alternatively, use staticField alone ('pkg.Cls.FIELD') and let the factory bean parse class+field from it.","Add a sanity check in test config to assert !(targetClass!=null && targetObject!=null) before initialization."],"exampleFix":"<!-- before: both targetClass and targetObject set -->\n<bean id='f' class='o.s.b.f.config.FieldRetrievingFactoryBean'>\n  <property name='targetClass' value='com.acme.Const'/>\n  <property name='targetObject' ref='someBean'/>\n  <property name='targetField' value='MAX'/>\n</bean>\n\n<!-- after: static field, targetObject removed -->\n<bean id='f' class='o.s.b.f.config.FieldRetrievingFactoryBean'>\n  <property name='targetClass' value='com.acme.Const'/>\n  <property name='targetField' value='MAX'/>\n</bean>","handlingStrategy":"validation","validationCode":"FieldRetrievingFactoryBean fb = new FieldRetrievingFactoryBean();\nif (fb.getTargetClass() != null && fb.getTargetObject() != null) {\n    throw new IllegalArgumentException(\"Set only one of targetClass/targetObject\");\n}","typeGuard":"boolean validTarget = (targetClass != null) ^ (targetObject != null);","tryCatchPattern":null,"preventionTips":["Set exactly one of targetClass or targetObject on FieldRetrievingFactoryBean.","Prefer the staticField shorthand for static constants to avoid this combination.","Add a configuration sanity check in tests before the context initializes.","Review XML copy-paste that leaves extra properties set."],"tags":["spring","factory-bean","configuration","static-field"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}