title: Reloading/Refreshing Spring configuration file and refresh value only for selected variables
body: <p>In spring app I'd like to have ability to change content of application.proporties and propagate these change to all bean values annotated by <em>@Value</em>.</p>
  
  <p>I know that I can simply reload all Spring context but I don't want to do this. Some of components can't be recreated. I want to reload values only for seleted bean proporties.</p>
  
  <p>I am using <em>Spring 3.2</em></p>
  
  <h2>My solution</h2>
  
  <p>I prototyped own solution but it requires use of reflection on some Spring internal classes. I'm looking for clearer solution</p>
  
  <p>Get required internal Sping objects and make public methods/fields which I need:</p>
  
  <pre><code>    TypeConverter typeConverter = beanFactory.getTypeConverter();
  
      // get object which store autowiring metadata for beans
      Object annotationProcessor = beanFactory
              .getSingleton("org.springframework.context.annotation.internalAutowiredAnnotationProcessor");
  
      // let it be public for my usage
      Method findMethod = ReflectionUtils.findMethod(AutowiredAnnotationBeanPostProcessor.class ,"findAutowiringMetadata", Class.class);
      ReflectionUtils.makeAccessible(findMethod);
  
      // let it be public for my usage
      Field checkedElements = ReflectionUtils.findField(InjectionMetadata.class, "checkedElements");
      ReflectionUtils.makeAccessible(checkedElements);
  </code></pre>
  
  <p>Iterate over all beans and search for elements with my own annotation (@Reloadable). Then reload values for them</p>
  
  <pre><code>for (String name : allBeanNames) {
  
      Object bean = beanFactory.getSingleton(name);
      InjectionMetadata metadata = (InjectionMetadata) findMethod.invoke(
              annotationProcessor, bean.getClass());
  
      @SuppressWarnings("unchecked")
      Set&lt;InjectedElement&gt; elements = (Set&lt;InjectedElement&gt;) checkedElements
              .get(metadata);
  
      for (InjectedElement element : elements) {
          Member member = element.getMember();
          // check for each elements if it has my own annotation which
          // means that it should be realoaded
          // check removed for simplify
  
          // get new value and load it
          String placeholder = valueAnnotation.value();
          String resolvedValue = env.resolvePlaceholders(placeholder);
          Object converted = typeConverter.convertIfNecessary(
                  resolvedValue, type, field);
  
          // load value
          if (member instanceof Method) {
              ((Method) member).invoke(bean, converted);
          } else if (member instanceof Field) {
              ((Field) member).set(bean, converted);
          }
  
      }
  }
  </code></pre>
  
  <h2>Other solutions</h2>
  
  <p><a href="http://www.wuenschenswert.net/wunschdenken/archives/107" rel="nofollow">http://www.wuenschenswert.net/wunschdenken/archives/107</a> - works for older version of spring</p>