spring-projects/spring-framework · error · UnsupportedOperationException

Only supports includeNonSingletons=true by default

Error message

Only supports includeNonSingletons=true by default

What it means

UnsupportedOperationException from the default ObjectProvider.stream(Predicate, boolean) at line 307 when includeNonSingletons is false. Since 6.2.5 this overload exists but the default implementation can only enumerate all matches (singletons + non-singletons); it cannot restrict to singletons, so passing false is rejected.

Solutions

  1. Call stream(filter) / stream(filter, true) (the default includeNonSingletons=true path) and filter singletons yourself via beanFactory.isSingleton(name).
  2. Override stream(Predicate, boolean) in your custom ObjectProvider to honour the flag.
  3. Use ListableBeanFactory.getBeansOfType(type, false, true) directly when singleton-only enumeration is required.

Example fix

// before
provider.stream(filter, false); // UnsupportedOperationException

// after
provider.stream(filter)
        .filter(o -> beanFactory.isSingleton(getBeanName(o)));
Defensive patterns

Strategy: validation

Validate before calling

// Only call the boolean overload with includeNonSingletons=true on default impls
if (!includeNonSingletons && !providerOverridesStream(provider, false)) {
    throw new UnsupportedOperationException("provider cannot restrict to singletons");
}

Prevention

When it happens

Trigger: Calling objectProvider.stream(filter, false) or .stream(UNFILTERED, false) on a default-method ObjectProvider (or any impl that does not override this overload) intending to iterate only singleton beans.

Common situations: Trying to mirror ListableBeanFactory.getBeansOfType(includeNonSingletons=false) semantics via an ObjectProvider; custom ObjectProvider subclasses that do not implement the boolean overload.

Related errors


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

Appendix: source

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

	default Stream<T> orderedStream(Predicate<Class<?>> customFilter) {
		return orderedStream(customFilter, true);
	}

	/**
	 * Return a custom-filtered {@link Stream} over all matching object instances,
	 * without specific ordering guarantees (but typically in registration order).
	 * @param customFilter a custom type filter for selecting beans among the raw
	 * bean type matches (or {@link #UNFILTERED} for all raw type matches without
	 * any default filtering)
	 * @param includeNonSingletons whether to include prototype or scoped beans too
	 * or just singletons (also applies to FactoryBeans)
	 * @since 6.2.5
	 * @see #stream(Predicate)
	 * @see #orderedStream(Predicate, boolean)
	 */
	default Stream<T> stream(Predicate<Class<?>> customFilter, boolean includeNonSingletons) {
		if (!includeNonSingletons) {
			throw new UnsupportedOperationException("Only supports includeNonSingletons=true by default");
		}
		return stream().filter(obj -> customFilter.test(obj.getClass()));
	}

	/**
	 * Return a custom-filtered {@link Stream} over all matching object instances,
	 * pre-ordered according to the factory's common order comparator.
	 * @param customFilter a custom type filter for selecting beans among the raw
	 * bean type matches (or {@link #UNFILTERED} for all raw type matches without
	 * any default filtering)
	 * @param includeNonSingletons whether to include prototype or scoped beans too
	 * or just singletons (also applies to FactoryBeans)
	 * @since 6.2.5
	 * @see #orderedStream()
	 * @see #stream(Predicate)
	 */
	default Stream<T> orderedStream(Predicate<Class<?>> customFilter, boolean includeNonSingletons) {
		if (!includeNonSingletons) {

View on GitHub (pinned to 69bf83ad71)