spring-projects/spring-framework · error · IllegalArgumentException

'sourceList' is required

Error message

'sourceList' is required

What it means

Thrown by ListFactoryBean.createInstance() when sourceList was never set. ListFactoryBean copies the provided source list into the target list instance, so without a source there is nothing to produce. This fires at bean creation (not init) time, i.e. when the List is first requested from the factory.

Solutions

  1. Provide a source list: in XML add <property name="sourceList"><list>...</list></property>; in code call fb.setSourceList(List.of(...)).
  2. For an empty list, pass an empty list explicitly: fb.setSourceList(new ArrayList<>()) or <property name="sourceList"><list/></property>.
  3. If you do not need Spring-managed sharing, inject a plain List directly instead of ListFactoryBean.

Example fix

// before (broken)
ListFactoryBean fb = new ListFactoryBean();
fb.getObject();
// after
ListFactoryBean fb = new ListFactoryBean();
fb.setSourceList(List.of("a", "b"));
Defensive patterns

Strategy: validation

Validate before calling

if (sourceList == null) {
    sourceList = List.of(); // or fail fast with a clear message
}
factoryBean.setSourceList(sourceList);

Type guard

static boolean hasSourceList(ListFactoryBean fb) {
    // sourceList has no public getter; track it at the call site
    return true; // ensure setSourceList was invoked before getObject()
}

Try / catch

try {
    return fb.getObject();
} catch (IllegalArgumentException ex) {
    if (ex.getMessage().contains("sourceList")) {
        fb.setSourceList(List.of()); // provide empty default then retry
        return fb.getObject();
    }
    throw ex;
}

Prevention

When it happens

Trigger: A <bean class="ListFactoryBean"> definition with no <property name="sourceList"> child (often when using the bean for an empty list and assuming it defaults). Programmatic ListFactoryBean used without setSourceList. A <util:list> backed by ListFactoryBean where the inner elements are all omitted incorrectly.

Common situations: Forgetting to populate the list, intending an empty list but providing no element. XML where the <list> child was accidentally removed. Refactoring that dropped the sourceList property.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/60efb09b3ca52a1c. Report an issue: GitHub.

Appendix: source

Thrown at spring-beans/src/main/java/org/springframework/beans/factory/config/ListFactoryBean.java:80

		}
		if (!List.class.isAssignableFrom(targetListClass)) {
			throw new IllegalArgumentException("'targetListClass' must implement [java.util.List]");
		}
		this.targetListClass = targetListClass;
	}


	@Override
	@SuppressWarnings("rawtypes")
	public Class<List> getObjectType() {
		return List.class;
	}

	@Override
	@SuppressWarnings("unchecked")
	protected List<Object> createInstance() {
		if (this.sourceList == null) {
			throw new IllegalArgumentException("'sourceList' is required");
		}
		List<Object> result = null;
		if (this.targetListClass != null) {
			result = BeanUtils.instantiateClass(this.targetListClass);
		}
		else {
			result = new ArrayList<>(this.sourceList.size());
		}
		Class<?> valueType = null;
		if (this.targetListClass != null) {
			valueType = ResolvableType.forClass(this.targetListClass).asCollection().resolveGeneric();
		}
		if (valueType != null) {
			TypeConverter converter = getBeanTypeConverter();
			for (Object elem : this.sourceList) {
				result.add(converter.convertIfNecessary(elem, valueType));
			}
		}

View on GitHub (pinned to 69bf83ad71)