title: Creating a singleton bean using runtime parameters
body: <p>I am new to the Spring framework and can't find a way to achieve the following:</p>
  
  <p>I am using a class whose attributes are all private and there are no setters (the intended way to use objects of that class is to set attributes once with a constructor) - I will refer to it as <em>Preferences</em>. I also have a few classes that each has the same instance of <em>Preferences</em> as an attribute. <em>Preferences</em> is intended to contain certain properties, among which some can only be resolved at runtime (e.g. provided by the user).</p>
  
  <p>In my .xml file I would write something along the lines of:</p>
  
  <pre><code>&lt;bean id="preferenes" class="Preferences" scope="singleton"&gt;
      &lt;constructor-arg index="0" value="defaultAttrOne" /&gt;
      &lt;constructor-arg index="1" value="defaultAttrTwo" /&gt;
      &lt;constructor-arg index="2" value="defaultAttrThree" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="someOtherBean" class="SomeOtherClass" scope="singleton"&gt;
      &lt;constructor-arg index="0" ref="preferences" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>That is, I could provide default values and replace some of them with custom ones at runtime. As I cannot modify attributes of an existing instance of <em>Preferences</em>, I would have to construct a new object and somehow make the instance of SomeOtherClass point to that new object (is this possible through the bean mechanism?).</p>
  
  <p>Rather, I'd pass the desired runtime constructor arguments to the <em>preferences</em> bean before instantiating any of the beans (those arguments will be known before the first call to the ApplicationContext's constructor). I know there is a flavour of the getBean() method that takes varargs as initialization parameters, though it only applies to prototype beans. In this case I want to initialize <em>Preferenes</em> once and have all helper classes refer to that single instance.</p>
  
  <p>Thank you for any hints.</p>