Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring scope references as enums? [duplicate]

Tags:

java

scope

spring

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")
like image 571
membersound Avatar asked Jan 28 '14 14:01

membersound


2 Answers

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.

like image 129
Konstantin Yovkov Avatar answered Oct 24 '22 03:10

Konstantin Yovkov


Constants that you can use to avoid literal Strings are:

ConfigurableBeanFactory.SCOPE_SINGLETON 
ConfigurableBeanFactory.SCOPE_PROTOTYPE
WebApplicationContext.SCOPE_REQUEST
WebApplicationContext.SCOPE_SESSION

source: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Scope.html

like image 33
hoodieman Avatar answered Oct 24 '22 03:10

hoodieman