spring-projects/spring-framework · error · IllegalArgumentException
Specify either targetClass or targetObject, not both
Error message
Specify either targetClass or targetObject, not both
What it means
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.
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.
Example fix
<!-- before: both targetClass and targetObject set --> <bean id='f' class='o.s.b.f.config.FieldRetrievingFactoryBean'> <property name='targetClass' value='com.acme.Const'/> <property name='targetObject' ref='someBean'/> <property name='targetField' value='MAX'/> </bean> <!-- after: static field, targetObject removed --> <bean id='f' class='o.s.b.f.config.FieldRetrievingFactoryBean'> <property name='targetClass' value='com.acme.Const'/> <property name='targetField' value='MAX'/> </bean>
Defensive patterns
Strategy: validation
Validate before calling
FieldRetrievingFactoryBean fb = new FieldRetrievingFactoryBean();
if (fb.getTargetClass() != null && fb.getTargetObject() != null) {
throw new IllegalArgumentException("Set only one of targetClass/targetObject");
} Type guard
boolean validTarget = (targetClass != null) ^ (targetObject != null);
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Specify targetClass or targetObject in combination with…
- staticField must be a fully qualified class plus static…
- Neither 'targetObject' nor 'targetBeanName' specified, and…
- Property 'serviceLocatorInterface' is required
- 'propertyPath' is required
AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09).
Data as JSON: /api/errors/8ef9b3a143a3e092.
Report an issue: GitHub.
Appendix: source
Thrown at spring-beans/src/main/java/org/springframework/beans/factory/config/FieldRetrievingFactoryBean.java:164
* nor "targetField" have been specified.
* This allows for concise bean definitions with just an id/name.
*/
@Override
public void setBeanName(String beanName) {
this.beanName = StringUtils.trimAllWhitespace(BeanFactoryUtils.originalBeanName(beanName));
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
@Override
@SuppressWarnings("NullAway") // Dataflow analysis limitation
public void afterPropertiesSet() throws ClassNotFoundException, NoSuchFieldException {
if (this.targetClass != null && this.targetObject != null) {
throw new IllegalArgumentException("Specify either targetClass or targetObject, not both");
}
if (this.targetClass == null && this.targetObject == null) {
if (this.targetField != null) {
throw new IllegalArgumentException(
"Specify targetClass or targetObject in combination with targetField");
}
// If no other property specified, consider bean name as static field expression.
if (this.staticField == null) {
this.staticField = this.beanName;
Assert.state(this.staticField != null, "No target field specified");
}
// Try to parse static field into class and field.
int lastDotIndex = this.staticField.lastIndexOf('.');
if (lastDotIndex == -1 || lastDotIndex == this.staticField.length()) {
throw new IllegalArgumentException(View on GitHub (pinned to 69bf83ad71)