2011-03-21 53 views
5

或者,我可以將一個自定義的org.springframework.beans.factory.config.Scope接口綁定到特定的@Scope -annotated註釋嗎?是否有可能用JSR-330 @Scope變體替換Spring @Scope(「request」)?

舉例來說,我已經定製了新的範圍類型:

@javax.inject.Scope @Retention(RUNTIME) 
@interface Conversation {} 

class ConversationScope implements Scope { ... } 

class ConversationScopeConfigurer extends BeanFactoryPostProcessor 
    { beanFactory.registerScope("conversation", new ConversationScope()); } 

現在我想用它作爲,

@Component 
@Conversation 
class Topic { ... } 

代替,

@Component 
@org.springframework.context.annotation.Scope("conversation") 
class Topic { ... } 

是否有可能?

在spring-context中是否有類似「AnnotationPostProcessor」的東西?

+0

如果使用''或''開箱即可使用,那麼它可能根本無法工作。 – skaffman 2011-03-21 19:03:28

回答

8

這似乎與你的<context:component-scan>

註冊custom scope resolver例如成爲可能:

<context:component-scan base-package="com.company" scope-resolver="org.springframework.context.annotation.Jsr330ScopeMetadataResolver" /> 

也看到了bridge for JSR-299 annotations的這個例子中,如果你需要定製您的解決方案多一點點。

+0

+1爲偉大的例子。 – 2011-03-21 22:25:11