How to extend already defined lists and maps in Spring application context?

后端 未结 2 1016
轻奢々
轻奢々 2021-02-02 14:45

Imagine a staged application context with different phases. We start with an early phase to define the necessary infrastructure. The xml application contexts are loaded sequenti

相关标签:
2条回答
  • 2021-02-02 15:33

    You can define exampleMapping is the second definition is in a separate file, and you use <import resource="..."/> to import one file into another, but it's a fragile approach, and easily broken.

    I suggest a more robust strategy. Replace the exampleMapping with a Registry class, which in turn contains and manages the mappings:

    public MappingRegistry<K,V> {
    
       private final Map<K,V> mappings = new HashMap<K,V>();
    
       public void addMapping(K key, V value) {
          mappings.put(key, value);
       }
    
       public Map<K,V> getMappings() {
          return Collections.unmodifiableMap(mappings);
       }
    }
    

    Then, write a class which registers a mapping with the registry:

    public class MappingRegistrar<K,V> {
    
       private final MappingRegistry<K,V> registry;
    
       private K key;
       private V value;
    
       @Autowired
       public MappingRegistrar(MappingRegistry<K,V> registry) {
          this.registry = registry;
       }
    
       public void setKey(K key) {
          this.key = key;
       }
    
       public void setValue(V value) {
          this.value = value;
       }
    
       @PostConstruct
       public void registerMapping() {
          registry.addMapping(key, value);
       }
    }
    

    The your config becomes something like this:

    <bean id="mappingRegistry" class="com.xyz.MappingRegistry"/>
    
    <bean id="mappingA" class="com.xyz.MappingRegistrar" p:key="keyA" p:value="valueA"/>
    <bean id="mappingB" class="com.xyz.MappingRegistrar" p:key="keyB" p:value="valueB"/>
    <bean id="mappingC" class="com.xyz.MappingRegistrar" p:key="keyC" p:value="valueC"/>
    

    These mappings can now be scattered throughout your config in any way you see fit, and they will self-assemble. ExampleServcie is then injected with the MappingRegistry and extracts the mappings accordingly.

    It's a bit more work than what you already have, but it's a lot more flexible and less error prone. This is particularly valuable if you're trying to build an extensible framework of some kind; you want to put fewer constraints on how people use it.

    0 讨论(0)
  • 2021-02-02 15:36

    Both map and list has this attribute named merge=true|false for merging two lists. Alternatively you can use MethodInvokingFactoryBean for calling already defined list's add method to add extra items later.

    Let's go over your example.

    1) First the second scenario with MethodInvokingFactoryBean. Instead of defining the way you do, I defined your beans a little differently.

    <bean class="java.util.HashMap" id="exampleMapping">
        <constructor-arg index="0">
            <map>
                <entry key="theKey" value="theValue"/>
            </map>
        </constructor-arg>
    </bean>
    
    <bean id="exampleService" class="com.stackoverflow.example.ExampleService">
        <property name="mapping" ref="exampleMapping"/>
    </bean>
    

    In another application content file, you can do the following to extend the map.

    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="exampleMapping"/>
        <property name="targetMethod" value="putAll"/>
        <property name="arguments">
            <list>
                <map id="exampleMapping">
                <entry key="theKey2" value="theValue2"/>
                <entry key="theKey3" value="theValue3"/>
                <map>
            </list>
        </property>
    </bean>
    

    2) Now the first scenario. For this one, I just found something on page http://forum.springsource.org/showthread.php?t=53358

    <bean id="commonData" class="A">
        <property name="map">
            <util:map>
                <entry key="1" value="1"/>
            </util:map>
        </property>
    </bean>
    <bean id="data" class="A" parent="commonData">
        <property name="map">
            <util:map merge="true">
                <entry key="2" value="2"/>
            </util:map>
        </property>
    </bean>
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题