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

Error "Specify either targetClass or targetObject, not both" thrown in spring-projects/spring-framework.

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 e8729d0438)

When it happens

Trigger: Thrown at spring-beans/src/main/java/org/springframework/beans/factory/config/FieldRetrievingFactoryBean.java:164 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04). Data as JSON: /data/errors/8ef9b3a143a3e092.json. Report an issue: GitHub.