Spring框架是否可以以注释驱动的方式注入集合?

发布于 2024-11-30 10:31:14 字数 468 浏览 0 评论 0原文

是否可以使用注释驱动注入执行相同的操作:

<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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你再特殊 2024-12-07 10:31:14

好问题 - 我不这么认为(假设“注释驱动注入”指的是 AnyAction 上的注释)。

以下方法可能有效,但我认为 Spring 无法识别 @Resources 注释:

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

无论如何,尝试一下,你永远不会知道。

除此之外,您可以使用 @Configuration 样式配置而不是 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;
   }
}

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;
   }
}
听你说爱我 2024-12-07 10:31:14

是的,如果您使用此模式,Spring 将愉快地注入所有已配置的拦截器:

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

请注意,您可能必须在 context.xml 上配置 default-autowire=byType。我不知道在普通注释配置中是否有替代方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文