spring-projects/spring-framework · error · UnsupportedOperationException

Retrieval with arguments not supported -for custom…

Error message

Retrieval with arguments not supported -for custom ObjectProvider classes, implement getObject(Object...) for your purposes

What it means

UnsupportedOperationException from the default ObjectProvider.getObject(Object... args) at line 109. The interface ships a no-arg getObject() default but does NOT implement the args variant; only the framework's internal ContextBeanProvider-style implementations support retrieval with explicit construction arguments. Custom ObjectProvider subclasses must override it.

Solutions

  1. Override getObject(Object... args) in your custom ObjectProvider to perform the actual retrieval.
  2. Avoid the args overload; use getObject() and configure the bean definition with the desired constructor args instead.
  3. Inject the BeanFactory directly and use getBean(name, args) when runtime args are essential.

Example fix

// before
public class TestProvider<T> implements ObjectProvider<T> {
    // only getObject() implemented; getObject(args) -> UnsupportedOperationException
}

// after
public T getObject(Object... args) {
    return beanFactory.getBean(type, args);
}
Defensive patterns

Strategy: validation

Validate before calling

// Detect the unsupported args path before calling
Method m = provider.getClass().getMethod("getObject", Object[].class);
if (m.isDefault()) {
    throw new UnsupportedOperationException("provider does not support getObject(args)");
}

Type guard

static boolean supportsArgsLookup(ObjectProvider<?> p) {
    try {
        return !p.getClass().getMethod("getObject", Object[].class).isDefault();
    } catch (NoSuchMethodException e) { return false; }
}

Prevention

When it happens

Trigger: Calling objectProvider.getObject(args...) on a hand-rolled ObjectProvider implementation, or on a default-method-only test double, that did not override the args overload.

Common situations: Unit tests faking an ObjectProvider; library code that accepts ObjectProvider from a caller and tries to pass constructor args; extending ObjectProvider without realising getObject(args) is not defaulted.

Related errors


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

Appendix: source

Thrown at spring-beans/src/main/java/org/springframework/beans/factory/ObjectProvider.java:109

		T result = it.next();
		if (it.hasNext()) {
			throw new NoUniqueBeanDefinitionException(Object.class, 2, "more than 1 matching bean");
		}
		return result;
	}

	/**
	 * Return an instance (possibly shared or independent) of the object
	 * managed by this factory.
	 * <p>Allows for specifying explicit construction arguments, along the
	 * lines of {@link BeanFactory#getBean(String, Object...)}.
	 * @param args arguments to use when creating a corresponding instance
	 * @return an instance of the bean
	 * @throws BeansException in case of creation errors
	 * @see #getObject()
	 */
	default T getObject(@Nullable Object... args) throws BeansException {
		throw new UnsupportedOperationException("Retrieval with arguments not supported -" +
				"for custom ObjectProvider classes, implement getObject(Object...) for your purposes");
	}

	/**
	 * Return an instance (possibly shared or independent) of the object
	 * managed by this factory.
	 * @return an instance of the bean, or {@code null} if not available
	 * @throws BeansException in case of creation errors
	 * @see #getObject()
	 */
	default @Nullable T getIfAvailable() throws BeansException {
		try {
			return getObject();
		}
		catch (NoUniqueBeanDefinitionException ex) {
			throw ex;
		}
		catch (NoSuchBeanDefinitionException ex) {

View on GitHub (pinned to 69bf83ad71)