简体   繁体   English

在Spring bean列表中添加额外的值

[英]Adding extra values into a spring bean list

I have a list of implementations of an interface managed by Spring in the main module of my project: 我在项目的主要模块中列出了Spring管理的接口的实现:

  <bean id="mail-properties-service" class="com.mail.wrapper.MailPropertiesWrapper" init-method="flushProperties">
    <property name="propertiesService" ref="application-properties-service"/>
  </bean>
  <bean id="record-properties-service" class="com.record.wrapper.RecordPropertiesWrapper" init-method="flushProperties">
    <property name="propertiesService" ref="application-properties-service"/>
  </bean>
  <bean id="admin-properties-service" class="com.admin.wrapper.SystemAdminPropertiesWrapper" init-method="flushProperties">
    <property name="propertiesService" ref="application-properties-service"/>
  </bean>
  <bean id="user-properties-service" class="com.user.wrapper.UserAdminPropertiesWrapper" init-method="flushProperties">
    <property name="propertiesService" ref="application-properties-service"/>
  </bean>
  <bean id="email-campaign-properties-service" class="com.email.wrapper.EmailCampaignPropertiesWrapper" init-method="flushProperties">
    <property name="propertiesService" ref="application-properties-service"/>
  </bean>

  <util:list id="properties-wrappers" value-type="com.wrapper.AbstractPropertiesWrapper">
    <ref bean="mail-properties-service"/>
    <ref bean="record-properties-service"/>
    <ref bean="admin-properties-service"/>
    <ref bean="user-properties-service"/>
    <ref bean="email-campaign-properties-service"/>
  </util:list>

This list is used by a general wrapper manager to issue clear cache instructions when properties change, plus other things. 通用包装管理器使用此列表在属性更改以及其他更改时发出明确的缓存指令。

I also have extensions to the project, for instance a SOP module. 我还对该项目进行了扩展,例如一个SOP模块。 It would be nice somewhere in the applicationContext.xml file in the SOP module to have something like this: SOP模块中的applicationContext.xml文件中的某处最好具有以下内容:

  <bean id="sop-properties-service" class="com.sop.wrapper.SOPPropertiesWrapper" init-method="flushProperties">
    <property name="propertiesService" ref="application-properties-service"/>
  </bean>

  <bean ref="properties-wrappers">
    <add-item ref="sop-properties-service"/>
  </bean>

This way, the main project config doesnt have to know about the SOP module, and the module can add it's wrapper to the wrappers list. 这样,主项目配置不必了解SOP模块,并且该模块可以将其包装器添加到包装器列表。 Is there any way to do this? 有什么办法吗?

Thanks in advance. 提前致谢。

For those in a similar situation, I've found a simple solution: use Spring Autowiring, which will find all implementations of a given interface or class in the configuration and put them into an array. 对于那些处于类似情况的人,我找到了一个简单的解决方案:使用Spring Autowiring,它将在配置中找到给定接口或类的所有实现,并将它们放入数组中。

  1. Define the properties classes in the xml (or as @Component ) as in the example in the question. 如问题示例中所示,在xml中定义属性类(或定义为@Component )。
  2. In the wrapper manager, add the following code. 在包装管理器中,添加以下代码。

     @Autowired protected AbstractPropertiesWrapper[] wrappers; 

The wrappers array will contain all wrappers, including the SOP one, without the main module needing to know anything about the SOP project. wrappers数组将包含所有包装器,包括一个SOP,而主模块不需要了解有关SOP项目的任何信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM