Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You <em>can</em> define <code>exampleMapping</code> is the second definition is in a separate file, and you use <code>&lt;import resource="..."/&gt;</code> to import one file into another, but it's a fragile approach, and easily broken.</p> <p>I suggest a more robust strategy. Replace the <code>exampleMapping</code> with a <code>Registry</code> class, which in turn contains and manages the mappings:</p> <pre><code>public MappingRegistry&lt;K,V&gt; { private final Map&lt;K,V&gt; mappings = new HashMap&lt;K,V&gt;(); public void addMapping(K key, V value) { mappings.put(key, value); } public Map&lt;K,V&gt; getMappings() { return Collections.unmodifiableMap(mappings); } } </code></pre> <p>Then, write a class which registers a mapping with the registry:</p> <pre><code>public class MappingRegistrar&lt;K,V&gt; { private final MappingRegistry&lt;K,V&gt; registry; private K key; private V value; @Autowired public MappingRegistrar(MappingRegistry&lt;K,V&gt; 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); } } </code></pre> <p>The your config becomes something like this:</p> <pre><code>&lt;bean id="mappingRegistry" class="com.xyz.MappingRegistry"/&gt; &lt;bean id="mappingA" class="com.xyz.MappingRegistrar" p:key="keyA" p:value="valueA"/&gt; &lt;bean id="mappingB" class="com.xyz.MappingRegistrar" p:key="keyB" p:value="valueB"/&gt; &lt;bean id="mappingC" class="com.xyz.MappingRegistrar" p:key="keyC" p:value="valueC"/&gt; </code></pre> <p>These mappings can now be scattered throughout your config in any way you see fit, and they will self-assemble. <code>ExampleServcie</code> is then injected with the <code>MappingRegistry</code> and extracts the mappings accordingly.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload