micronaut-core icon indicating copy to clipboard operation
micronaut-core copied to clipboard

Consider aligning default scope for @Bean when you use it to mark a class or via a factory

Open sdelamo opened this issue 2 years ago • 3 comments

This would be @Prototype scope:

@Bean
class Robot {
}

however, the following code defaults to@Singleton:

@Factory
class RobotFactor {
@Bean
Robot create() {
    return new Robot();
}
}

I think it would be less confusing to have them both default to the same scope. Maybe,

@Bean
class Robot {
}

should be a @Singleton in Micronaut 4?

sdelamo avatar Sep 21 '22 07:09 sdelamo

We also need to define what annotations are inherited to the bean from the factory method and class.

dstepanov avatar Sep 21 '22 07:09 dstepanov

it is a bug that the factory version defaults to singleton IMO

graemerocher avatar Sep 21 '22 08:09 graemerocher

The factory class itself is a singleton, which looks correct to me. What are prototypes are the beans created by it

alvarosanchez avatar Sep 21 '22 09:09 alvarosanchez