开发者

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?


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;
   }
}


Yes, Spring will happily inject all configured interceptors if you use this pattern:

@Autowired
public void setInterceptors(List<Interceptor> interceptors){
    this.interceptors = interceptors;
}
private List<Interceptor> interceptors;

Note that you will probably have to configure default-autowire=byType on your context.xml. I don't know if there's an alternative to this in plain annotation configuration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜