简体   繁体   中英

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:

  <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. It would be nice somewhere in the applicationContext.xml file in the SOP module to have something like this:

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

  1. Define the properties classes in the xml (or as @Component ) as in the example in the question.
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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