Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Spring framework makes possible to inject a collection in annotation-driven fashion?

Is it possible to do the same using annotation-driven injection:

<beans>
...
    <bean id="interceptorsList" class="com.mytest.AnyAction">
        <property name="interceptors">
            <list>
                <ref bean="validatorInteceptor"/>
                <ref bean="profilingInterceptor"/>
            </list>
        </property>
    </bean>
</beans>

Is it possible to do the same using annotation-driven injection?

like image 350
dave_carmi Avatar asked Aug 21 '11 14:08

dave_carmi


1 Answers

Good question - I don't think so (assuming that by "annotation-driven injection" you're referring to annotations on AnyAction).

It's possible that the following might work, but I don't think Spring recognises the @Resources annotation:

@Resources({
   @Resource(name="validatorInteceptor"),
   @Resource(name="profilingInterceptor")
})
private List interceptors;

Give it a try anyway, you never know.

Other than, you can use @Configuration-style configuration instead of XML:

@Configuration
public class MyConfig {

   private @Resource Interceptor profilingInterceptor;
   private @Resource Interceptor validatorInteceptor;

   @Bean
   public AnyAction anyAction() {
      AnyAction anyAction = new AnyAction();
      anyAction.setInterceptors(Arrays.asList(
        profilingInterceptor, validatorInteceptor
      ));
      return anyAction;
   }
}
like image 158
skaffman Avatar answered Oct 15 '22 11:10

skaffman