{"record":{"id":"6df61f38fd93992c","repo":"spring-projects/spring-framework","slug":"element-access-not-supported-for-custom-objectpr","errorCode":null,"errorMessage":"Element access not supported - for custom ObjectProvider classes, implement stream() to enable all other methods","messagePattern":"Element access not supported - for custom ObjectProvider classes, implement stream\\(\\) to enable all other methods","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/ObjectProvider.java","lineNumber":237,"sourceCode":"\t */\n\t@Override\n\tdefault Iterator<T> iterator() {\n\t\treturn stream().iterator();\n\t}\n\n\t/**\n\t * Return a sequential {@link Stream} over all matching object instances,\n\t * without specific ordering guarantees (but typically in registration order).\n\t * <p>Note: The result may be filtered by default according to qualifiers on the\n\t * injection point versus target beans and the general autowire candidate status\n\t * of matching beans. For custom filtering against type-matching candidates, use\n\t * {@link #stream(Predicate)} instead (potentially with {@link #UNFILTERED}).\n\t * @since 5.1\n\t * @see #iterator()\n\t * @see #orderedStream()\n\t */\n\tdefault Stream<T> stream() {\n\t\tthrow new UnsupportedOperationException(\"Element access not supported - \" +\n\t\t\t\t\"for custom ObjectProvider classes, implement stream() to enable all other methods\");\n\t}\n\n\t/**\n\t * Return a sequential {@link Stream} over all matching object instances,\n\t * pre-ordered according to the factory's common order comparator.\n\t * <p>In a standard Spring application context, this will be ordered\n\t * according to {@link org.springframework.core.Ordered} conventions,\n\t * and in case of annotation-based configuration also considering the\n\t * {@link org.springframework.core.annotation.Order} annotation,\n\t * analogous to multi-element injection points of list/array type.\n\t * <p>The default method applies an {@link OrderComparator} to the\n\t * {@link #stream()} method. You may override this to apply an\n\t * {@link org.springframework.core.annotation.AnnotationAwareOrderComparator}\n\t * if necessary.\n\t * <p>Note: The result may be filtered by default according to qualifiers on the\n\t * injection point versus target beans and the general autowire candidate status\n\t * of matching beans. For custom filtering against type-matching candidates, use","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/ObjectProvider.java#L219-L255","documentation":"UnsupportedOperationException from the default ObjectProvider.stream() at line 237. iterator(), orderedStream(), forEach, and the new Predicate overloads all derive from stream(); a custom ObjectProvider that overrides only getObject() (inherited from ObjectFactory) leaves stream() unimplemented, so any iteration-based call fails.","triggerScenarios":"Calling iterator()/stream()/orderedStream()/forEach on a minimal ObjectProvider implementation that did not override stream(). The message tells you implementing stream() unlocks all derived element-access methods.","commonSituations":"Mockito mocks or stub ObjectProviders in tests; legacy ObjectProvider implementations written before Spring 5.1 (when stream() was introduced).","solutions":["Override stream() in your custom ObjectProvider to return the matching bean instances (e.g. Stream.of(bean)).","Use a real DependencyDescriptor/ContextBeanProvider-backed ObjectProvider instead of a hand-rolled one.","In tests, return Stream.of(...) or Collections.singletonList(bean).stream()."],"exampleFix":"// before\npublic class TestProvider<T> implements ObjectProvider<T> {\n    public T getObject() { return bean; } // iterator() -> UnsupportedOperationException\n}\n\n// after\n@Override public Stream<T> stream() { return Stream.of(bean); }","handlingStrategy":"validation","validationCode":"// Ensure stream() is overridden before iterating\nMethod s = provider.getClass().getMethod(\"stream\");\nif (s.isDefault()) {\n    throw new UnsupportedOperationException(\"provider must override stream()\");\n}","typeGuard":"static boolean supportsStream(ObjectProvider<?> p) {\n    try {\n        return !p.getClass().getMethod(\"stream\").isDefault();\n    } catch (NoSuchMethodException e) { return false; }\n}","tryCatchPattern":null,"preventionTips":["Always override stream() in custom ObjectProvider implementations.","In tests, return Stream.of(bean) or use Mockito to stub stream().","Prefer getObject() / getIfAvailable() over iteration when you only need one element."],"tags":["dependency-injection","object-provider","unsupported-operation"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}