Spring scope references as enums?

java, scope, spring

Solution

According to the Scope's documentation, the `value` element is of type `String`, not some enum constant. Hence we're searching for a class, where the possible values for the `value` element are exposed.

BeanDefinition is the class you're looking for. It provides several `public static String` fields, but you might be interested in these two:

SCOPE_SINGLETON
SCOPE_PROTOTYPE

And for example, they can be used like:

@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)

I would advice for re-using them, instead of setting Strings literals all the time, as you might do some typo.

Problem

Maybe I'm missing something, but isn't there any class that provides `Scope.PROTOTYPE, Scope.SINGLETON` static references? Or do I always have to use non-typesafe strings as scopes? ``` @Scope("prototype") @Scope("singleton") ```

Original source

Related problems