在Spring bean上下文中声明对象数组

亚历克斯·西米尼安(Alex Ciminian):

我正在尝试在Spring上下文文件中创建对象数组,因此可以将其注入声明为这样的构造函数中:

public RandomGeocodingService(GeocodingService... services) { }

我正在尝试使用<array>标签:

<bean id="googleGeocodingService" class="geocoding.GoogleGeocodingService">
 <constructor-arg ref="proxy" />
 <constructor-arg value="" />
</bean>

<bean id="geocodingService" class="geocoding.RandomGeocodingService">
    <constructor-arg>
        <array value-type="geocoding.GeocodingService">
            <!-- How do I reference the google geocoding service here? -->
        </array>
    </constructor-arg>
</bean>

我尚未在文档中找到如何执行此操作的示例或其他内容。另外,对于如何实现我想做的更好的方法,您有任何建议,请告诉我:)。

sk夫:

那是因为没有<array>,只有<list>

好消息是,Spring会根据需要在列表和数组之间进行自动转换,因此将数组定义为<list>,Spring会为您强制将其转换为数组。

这应该工作:

<bean id="googleGeocodingService" class="geocoding.GoogleGeocodingService">
   <constructor-arg ref="proxy" />
   <constructor-arg value="" />
</bean>

<bean id="geocodingService" class="geocoding.RandomGeocodingService">
    <constructor-arg>
        <list>
           <ref bean="googleGeocodingService"/>
        </list>
    </constructor-arg>
</bean>

如果需要,Spring还可以将单个bean强制转换为列表:

<bean id="geocodingService" class="geocoding.RandomGeocodingService">
    <constructor-arg>
       <ref bean="googleGeocodingService"/>
    </constructor-arg>
</bean>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Spring上下文中模拟bean

如何从Freemarker宏的上下文中获取Spring bean?

在Spring Web上下文中添加动态bean

Spring上下文中的多个AsyncAnnotationBeanPostProcessor

Bean的Spring会话范围如何工作?Web上下文中Bean的默认范围是什么?

为什么我无法使用Spring Boot在Spring上下文中获得Bean定义?

从Spring上下文中获取一个bean(以编程方式注册)?

从应用程序上下文中删除Spring Bean

在Spring Boot应用程序上下文中找不到Bean

Kotlin Spring无法在应用程序上下文中注册Bean

在Spring上下文中从异步方法调用bean的方法是否真的是并发的?

Spring MVC在应用程序上下文中刷新数据库Bean

向Spring安全上下文中存储的主体对象添加其他详细信息

从Spring MVC控制器的安全上下文中获取UserDetails对象

Spring Bean上下文中有许多未使用的bean是否会浪费大量资源?

Spring:依賴庫中的自動配置bean未在Spring上下文中加載

Spring 集成上下文中的并发消费者

如何在Spring上下文中注入模拟

在Spring上下文中通过类路径引用文件

Spring @Configuration 在测试上下文中未被覆盖

在Spring上下文中没有找到类型类... myPackageHere ...的bean,也没有多个

有没有一种方法可以检查是否在spring上下文中定义了bean?

Spring 4 + Tiles(3 || 2.2.2)-有什么方法可以在视图上下文中访问bean?

如何从测试应用程序上下文中排除 Spring Boot 应用程序 bean?

Spring AOP-如何使在父上下文中定义的方面在子上下文中工作?

通过Spring Boot在同一上下文中创建的对象的两个不同的哈希码

spring-cloud上下文引导程序在主上下文中忽略禁用的spring shutdown钩子

从 Tasklet 加载 Spring 集成上下文并访问原型 bean

要确保Spring bean的非簧载上下文