Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>By default Spring beans are singletons, which means that once <code>bean="my_bean"</code> is created it is shared between other components e.g. shared between A => <code>bean id="root_a_a_a"</code> and B => <code>bean id="root_b_b_b"</code></p> <p>The answer to your question depends on what exactly you are trying to achieve. </p> <h2>Two Beans</h2> <p>If <code>bean="my_bean"</code> does not need to be shared between A and B, then create two beans:</p> <p>inject this one to A</p> <pre><code>&lt;bean id="myBeanX" class="My"&gt; &lt;property name="culprit" value="X"/&gt; &lt;/bean&gt; </code></pre> <p>and this one to B</p> <pre><code>&lt;bean id="myBeanY" class="My"&gt; &lt;property name="culprit" value="Y"/&gt; &lt;/bean&gt; </code></pre> <p>notice they both are instances of the same class. </p> <p>You can also inline them into collaborators (A / B) if you don't need them for anything else:</p> <pre><code>&lt;bean id="root_a_a_a"&gt; &lt;constructor-arg&gt; &lt;bean class="My"&gt; &lt;property name="culprit" value="X"/&gt; &lt;/bean&gt; &lt;/constructor-arg&gt; &lt;/bean&gt; </code></pre> <p>You can also have a factory bean that creates <code>root_a_a_a</code> given the property for a class <code>My</code>, but that would be an overkill.</p> <h2>Single Bean</h2> <p>In case A and B needs to share the exact same reference to <code>bean="my_bean"</code>, the question is: are you ok with A and B changing <code>my_bean</code>'s state after <code>my_bean</code> is created? Probably <strong>not</strong>. </p> <p>If you <em>are</em>, which would be 0.41172% chance, you can change <code>my_bean</code>'s value to whatever you need in A's or B's constructors => would <strong>not</strong> recommend</p> <p>Hence you either would go with the <code>Two Bean</code> approach (which is most likely what you want), or you would need to refactor a property for "X" and "Y" into another e.g. <code>myConfig</code> component.</p> <hr> <h3><em>EDIT after the question was edited</em></h3> <p>If <code>root_a</code> and <code>root_b</code> will <strong>not</strong> be used together in the same instance of the context,you can use Spring Profiles (<a href="https://stackoverflow.com/questions/9435246/spring-injecting-different-properties-file-according-to-profile/9440209#9440209">example</a>), or SpEL / Property Based solutions (<a href="https://stackoverflow.com/questions/9470551/spring-property-substitution-for-test-and-production/9471696#9471696">example</a>)</p> <p>e.g.</p> <pre><code>&lt;bean id="my_bean"&gt; &lt;property name="my_map"&gt; &lt;map&gt; &lt;entry key="p" value="${ENV_SYSTEM:X}" /&gt; &lt;/map&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>It will set it to <code>X</code> by default, unless a ENV_SYSTEM system variable is set (e.g. to <code>Y</code>).</p>
    singulars
    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.
 

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