tags:

views:

1147

answers:

2

Hi I created a custom sterotype @Action, and Spring has managed to detect it in the package scan I configured in the configurations.

The next step I would like to do is to tell Spring that all classes with @Action should be created with prototype, instead of Singleton.

My @Action interface is as follows:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Action {
}

I tried to mark it with @Scope("prototype") but that does not seem to help.

Is what I desire possible?

Kent

A: 

Unfortunately not with spring 2.5.X. Your @Component-annotation describes the role of the bean while the scope is a separate axis, so a role and scope descriptor typically have to be applied separately to the implementation class. When viewed in this way it makes some sense (edit: at least it did so for a few seconds, anyway)

I don't know how this will change i spring 3.x, which is not too far away. There seems to be some room for improvement.

krosenvold
+1  A: 

The context:component-scan can be configured with a custom scope-resolver, which implements org.springframework.context.annotation.ScopeMetadataResolver.

I created a custom scope-resolver that not only checks the bean for a @Scope annotation (with the default resolver of org.springframework.context.annotation.AnnotationScopeMetadataResolver), but looks up annotations of annotations too (recursively).

One thing to note though, that looking up annotations recursively might go into an endless loop, as java.lang.annotation.Documented is annotated with java.lang.annotation.Documented. Best to maintain a table that indicates which annotation has been looked up already.

Kent Lai
@Scope now works out of the box in Spring 3
Jason Gritman