Java / Micronaut Interview questions
What is the difference between @Singleton and @Prototype scope in Micronaut?
| @Singleton | @Prototype |
| One shared instance for the entire ApplicationContext. | A new instance created on every injection or bean lookup. |
| Best for stateless, shareable services and clients. | Best for stateful or short-lived objects that shouldn't be shared. |
| Must be explicitly declared with @Singleton. | The implicit default scope when no scope annotation is present. |
Because @Prototype is Micronaut's default, a plain class with no scope annotation behaves like @Prototype automatically, which differs from Spring, where the default bean scope is singleton. Forgetting this is a common source of unexpected object creation when developers coming from Spring assume an unscoped class is shared.
More Related questions...