Skip to content

Re-allow merge of util:map list, set, ... based on the same bean ID [SPR-5199] #9872

Closed

Description

@spring-projects-issues

Thomas Timbul opened SPR-5199 and commented

For the sake of clarity/modularisation in my XML I am trying to separate out certain service definitions into separate files. It would be nice to see the re-emergence of 'merge' on the util: collection definitions in a way that definitions with the same ID are merged.

For example (hope this submits in a readable way):

[services.xml]
<util:map id="serviceMap" merge="true" />
<bean id="serviceProvider">
<property name="serviceMap" ref="serviceMap" />
</bean>

[service1.xml]
<util:map id="serviceMap" merge="true">
<entry key="S1" ref="service1" />
</util:map>
<bean id="service1" ..... />

[service2.xml]
<util:map id="serviceMap" merge="true">
<entry key="S2" ref="service2" />
</util:map>
<bean id="service2" ..... />

There are probably workarounds for this, but for the moment I would be forced to have the serviceMap defined completely in a single place.


Issue Links:

3 votes, 2 watchers

Activity

  1. spring-projects-issues commented on Apr 12, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Philip Graham commented

    Not sure if this is still an issue in more recent versions of Spring but for those of us stuck using 2.5 here is a workaround I've come up with:

    [services.xml]
    <bean id="serviceProvider" class="com.company.server.MyServiceImpl" />

    [service1.xml]
    <bean class="com.company.server.MyServiceProcedureProvider">
    <property name="procedures">
    <map>
    <entry key="com.company.mod1.server.ServiceProvider1" value-ref="serviceProvider1" />
    ...
    </map>
    </property>
    </bean>

    <bean id="serviceProvider1" class="com.company.mod1.server.ServiceProvider1" >
    ...
    </bean>

    [service2.xml]
    <bean class="com.company.server.MyServiceProcedureProvider">
    <property name="procedures">
    <map>
    <entry key="com.company.mod2.server.ServiceProvider2" value-ref="serviceProvider2" />
    ...
    </map>
    </property>
    </bean>

    <bean id="serviceProvider2" class="com.company.mod1.server.ServiceProvider2" >
    ...
    </bean>

    Then the MyServiceImpl class implements ApplicationContextAware and uses getBeansOfType() to merge the maps together:

    public class MyServiceImpl implements RemoteService, ApplicationContextAware {

    private Map procedures = new HashMap();

    public void setApplicationContext(ApplicationContext ac) {
    Map providers = ac.getBeansOfType(MyServiceProcedureProvider.class);
    for (Object provider : providers.values()) {
    procedures.putAll((MyServiceProcedureProvider) provider).getProcedures());
    }
    }

    /*

    • =========================================================================
    • Service Implementation
    • =========================================================================
      */

    // ... Delegate the service provider
    }

    The MyServiceProcedureProvider class is a POJO that encapsulates a Map with a
    getter and setter.

    NOTE: This solution has been made specifically for a single service that implements the command pattern but can be genericized for any situation similar to that described in the original post.

  2. spring-projects-issues commented on Apr 3, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Duplicates #11189

  3. added
    type: enhancement
    status: duplicate
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: duplicatetype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions