title: Configure Spring to set system properties 'before' a bean is initialised
body: <p>In a Spring MVC web application, i have a bean configured in the configuration file as:</p>
  
  <pre><code>&lt;bean class="com.callback.CallbackService"/&gt;
  </code></pre>
  
  <p>In the service class, the bean is initialised as shown below:</p>
  
  <pre><code>@Autowired
  CallbackService service
  </code></pre>
  
  <p>The CallbackService shown above gets its connection properties by making the following three calls (This cannot be changed for now):</p>
  
  <pre><code>System.getProperty("user");
  System.getProperty("password");
  System.getProperty("connectionURL");
  </code></pre>
  
  <p>The service class in which the instance of CallbackService is declared has access to the above three values by reading from the properties file as shown below:</p>
  
  <pre><code>@Value("${user}")
  protected String userName;
  
  @Value("${password}")
  protected String password;
  
  @Value("${connection}")
  protected String connectionString;  
  </code></pre>
  
  <p>All i need to set the properties for the CallbackService is to set the system properties (after they have been initialised) as shown below:</p>
  
  <pre><code>System.setProperty("user", userName);
  System.setProperty("password", password);
  System.setProperty("connectionURL", connectionString);
  </code></pre>
  
  <p>The problem i am having however is the order in which the objects are being initialized. The properties are being initialised but it looks like the System.setProperty calls happen before Spring has ready them from the properties file. </p>
  
  <p>I have tried several solutions but it seems the CallbackService object is instantiated before the values have been read from the properties files and the calls to System.setProperty is called. </p>
  
  <p>The properties file is eventually being read as i can see the values if i access them from one of the @Controller methods. The problem is the point in which the properties are initialised and the CallbackService instance is intantiated. </p>
  
  <p>After several hours googling, i tried the following solutions but none seem to populate the System properties before initialization/Instantiation of the CallbackService instance</p>
  
  <ol>
  <li>Implementing <code>InitiazingBean</code> and setting the system properties within the
  <code>afterPropertiesSet()</code> method.</li>
  <li>Implementing <code>ApplicationListener</code> and setting the system properties within the <code>onApplicationEvent()</code> method.</li>
  <li>Setting <code>lazy-init=true</code> for the CallbackService bean definition in the XML.</li>
  <li>Setting system properties as described here <a href="https://stackoverflow.com/questions/3339736/set-system-property-with-spring-configuration-file">Set System Property With Spring Configuration File</a></li>
  </ol>
  
  <p>Point 4 above seems to be what i want but i didn't see any difference when i added the following (with the three properties i need) to my context file. </p>
  
  <pre><code>&lt;bean id="systemPrereqs"
      class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"&gt;
      &lt;property name="targetObject" value="#{@systemProperties}" /&gt;
      &lt;property name="targetMethod" value="putAll" /&gt;
      &lt;property name="arguments"&gt;
          &lt;!-- The new Properties --&gt;
          &lt;util:properties&gt;
              &lt;prop key="java.security.auth.login.config"&gt;/super/secret/jaas.conf&lt;/prop&gt;
          &lt;/util:properties&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>How can i ensure that the values are read from the properties file before the calls to <code>System.setProperty</code> are executed and only then should the CallbackService instance be instantiated?</p>
  
  <p>Thanks</p>