title: How to Inject property values into a static property or method of an enum or normal class using Spring?
body: <p>I am in serious trouble due to a situation. Here's my problem.</p>
  
  <p><strong>PROBLEM:</strong> Need to inject a simple static string  field /property in an enum class FROM spring config XML.
  Here's some similar code I tested :      </p>
  
  <pre><code>public enum AppProperty {
      NUMBER_OF_ACCOUNTS("accounts"), NUMBER_OF_USERS("users");
  
      private AppProperty(String cuser) {
          this.current_user = cuser;
      }
  
      private final String current_user;
      private static final String PROPERTY_FILE = "application.properties";
  
      static {
          init();
      }
  
      public static String appContext; // **PROBLEM** : need to populate this ONLY
                                          // from spring config XML via property
                                          // injection.
  
      public static void init() {
  
          if (appContext.equals("statement")) {
              // do something..... generate statement et...
          } else {
              // do some other process... generate balance etc
          }
      }
  
      ..........  few more static methods...
      ..... few more non-static methods
  }// end of class
  </code></pre>
  
  <p>I tried mostly everything for this simple problem but strangely nothing seems working for Spring 3.1  :(</p>
  
  <p>Injecting a property via property setter etc not working at all..
  String appContext  always turns out NULL... 
  tried ..</p>
  
  <pre><code> @Autowired(required = true)
     setAppConfig( String context){
  AppProperty.appContext = context
  )
  }
  </code></pre>
  
  <p>and  tried  every wiring and autowiring.... mostly either returns null or exception "AppProperties.init() cannot be called as no default constructor ...."
  (even tried a default constructor for god's sake but don't work...!!!</p>
  
  <p>So thought lets get this single property from a property file and get it done but that also don't work and same problem.</p>
  
  <pre><code>&lt;context:component-scan base-package="com.my.package" /&gt; 
  &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="locations"&gt;
          &lt;list&gt;
              &lt;value&gt;classpath:application.properties&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>and used this in java class over propert .. @Value("${appContext}")</p>
  
  <p>This also demands to instantiate AppProperty.init() which is not achievable by me.</p>
  
  <p>I think the ONLY way to inject a property from config xml seems using
  MethodInvokingFactoryBean 
  (<a href="https://stackoverflow.com/questions/11324372/how-to-make-spring-inject-value-into-a-static-field">How to make spring inject value into a static field</a>)</p>
  
  <p>I tried this also but my hard luck it's also not working!!!!
  Only thing i think I may do wrong is not using the @value tag properly</p>
  
  <p>In config file I used :</p>
  
  <pre><code>&lt;bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"&gt;
      &lt;property name="staticMethod" value="com.my.package.setAppContext"/&gt;
      &lt;property name="arguments"  value ="testing"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>In java file  AppProperty I used </p>
  
  <pre><code>public static  String appContext;
    public void setAppContext(String appContext) {
      AppProperty.appContext = appContext;
      }
  </code></pre>
  
  <p><strong>Request</strong> : Please provide your answer with exact code which I can use as I may be using right solution but Syntex and configuration are missing ..
  Shall I use @Component over my AppProperty class!! etc... please provide details and get my blessings...</p>
  
  <p>Note: I NEED to Inject this property via Spring Config XML so that I can change the context at runtime.. As I am not allowed to change existing implementation of this class.
  So please don't suggest to remove enum class with something else...
  Thanks in advance.</p>