title: Java Spring - how to inject @Value to a data object?
body: <p>I have a data object (with getters\setter only) that needs to be aware of the Spring profile, i.e.</p>
  
  <pre><code>@Value("${spring.profiles.active}")
  private String profile;
  </code></pre>
  
  <p>I added a logic to one of it's 'set' method that checks the profile, i.e.</p>
  
  <pre><code>public void setItem(Item msg) {
      if (environmentProperties.isDevMode()) {
          this.msg= msg;
      }
  }
  </code></pre>
  
  <p>since this class is often marshal\unmarhsalled externally, so, of course the @Value isn't being populated - sine I didn't use spring Autowire to create the class instance... I tried defined the class as component, and autowire to an external class that holds the profile @Value - but it doesn't work
  I use spring 3.2 - with no XML definition.</p>
  
  <p>any suggestions?</p>
  
  <p>b.t.w.
  that data-objects often wrapped inside an exception class - so when it's created the profile should also be known to the data-object... </p>
  
  <p>thanks!</p>
  
  <p>EDITED:</p>
  
  <ul>
  <li>using ApplicationContextAware doesn't work - I get null the 'setApplicationContext' method is never invoked.</li>
  <li>also trying to get context directly doesn't work - get null instead when using:
  'ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();'</li>
  </ul>
  
  <p>FIXED:
  I've eventually found an example how to access the context staticly from an external class:</p>
  
  <pre><code>@Configuration
  public class ApplicationContextContainer implements ApplicationContextAware {
  
      private static ApplicationContext CONTEXT;
  
      /**
       * This method is called from within the ApplicationContext once it is
       * done starting up, it will stick a reference to itself into this bean.
       *
       * @param context a reference to the ApplicationContext.
       */
      @Override
      public void setApplicationContext(ApplicationContext context) throws BeansException {
          CONTEXT = context;
      }
  
      /**
       * This is about the same as context.getBean("beanName"), except it has its
       * own static handle to the Spring context, so calling this method statically
       * will give access to the beans by name in the Spring application context.
       * As in the context.getBean("beanName") call, the caller must cast to the
       * appropriate target class. If the bean does not exist, then a Runtime error
       * will be thrown.
       *
       * @param beanName the name of the bean to get.
       * @return an Object reference to the named bean.
       */
      public static Object getBean(String beanName) {
          return CONTEXT.getBean(beanName);
      }
  </code></pre>