Spring范围引用为枚举?

mem*_*und 10 java spring scope

也许我错过了什么,但是没有提供Scope.PROTOTYPE, Scope.SINGLETON静态引用的类吗?

或者我总是必须使用非类型安全的字符串作为范围?

@Scope("prototype")
@Scope("singleton")
Run Code Online (Sandbox Code Playgroud)

hoo*_*man 15

可以用来避免文字字符串的常量是:

ConfigurableBeanFactory.SCOPE_SINGLETON 
ConfigurableBeanFactory.SCOPE_PROTOTYPE
WebApplicationContext.SCOPE_REQUEST
WebApplicationContext.SCOPE_SESSION
Run Code Online (Sandbox Code Playgroud)

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


Kon*_*kov 11

根据Scope的文档,value元素是类型String,而不是枚举常量.因此,我们正在搜索一个类,其中value公开了元素的可能值.

BeanDefinition是您正在寻找的类.它提供了几个public static String字段,但您可能对这两个字段感兴趣:

SCOPE_SINGLETON
SCOPE_PROTOTYPE
Run Code Online (Sandbox Code Playgroud)

例如,它们可以像以下一样使用:

@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
Run Code Online (Sandbox Code Playgroud)

我建议重新使用它们,而不是一直设置字符串文字,因为你可能会做一些错字.