Spring: A prototype bean injected into a singleton is resolved once

The singleton is created once, so its dependency is injected once - you get the same prototype instance for ever.

Code
@Component @Scope("prototype")
class Task { }

@Component
class Runner {
    @Autowired private Task task;          // resolved ONCE
    @Autowired private ObjectProvider<Task> provider; // fresh each call

    void go() {
        System.out.println(task.hashCode());              // same every time
        System.out.println(provider.getObject().hashCode()); // different
    }
}
Output
1829164700
1553975225
1829164700
865113938
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11