title: Setting the value of one bean to be another bean in Java Spring
body: <p>I am having two main issues with Spring bean configuration:
  1. The properties I read through a properties file are not persisting when I retrieve the bean from my application context.
  2. I'm having trouble figuring out how to set the value of a bean as another bean- in my below example, I'm trying to set the Plan bean's Metric property to be a certain bean.</p>
  
  <p>I have two small classes, a Metric, and a larger Plan:</p>
  
  <pre><code>@Component
  public class Metric {
  
     @Value("${nameOfMetric}")
     String name;
  
     @Value("${empID}")
     String empID;
  
     @Value("${value:0.0}")
     Double value;
     }
  </code></pre>
  
  <p>And a larger Plan object which has three different Metrics as properties:</p>
  
  <pre><code>public class Plan {
  
  public Metric hoursWorked;
  
  public Metric monthlyDeals;
  
  public Metric monthlyGoal;
  ...}
  </code></pre>
  
  <p>I also have a properties file that I am reading from:</p>
  
  <pre><code>nameOfMetric = Untitled Metric
  empID = Unknown
  hoursWorked = 120.0
  monthlyDeals = 40.0
  monthlyGoal = 100.0
  </code></pre>
  
  <p>Is there a way for me to create beans for each of the metrics (hoursWorked, monthlyDeals, monthlyGoal), and then inject each of these beans into a Plan bean? So far, I have tried to using a PropertyConfig class with the @Configuration annotation: </p>
  
  <pre><code>@Inject
  private Environment environment;
  
  private Metric injectMetric(String propertyName){
  
      Metric metric = new Metric();
      metric.setEmpID(environment.getProperty("empID"));
      metric.setName(propertyName);
      metric.setValue(Double.parseDouble(environment.getProperty(propertyName)));
      System.out.println("Metric injected: " + metric.toString());
      return metric;
  }
  
  @Bean
  public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
  }
  
  @Bean(name = {"hoursWorkedBean"})
  @Scope("prototype")
  public Metric hoursWorked() {
      Metric metric = injectMetric("hoursWorked");
      return metric;
  }
  </code></pre>
  
  <p>But when I try to access this bean, I get the default or null values in my main application, signalling that these properties have not persisted:</p>
  
  <pre><code>    Metric hoursWorked = (Metric) ctx.getBean("hoursWorkedBean");
  
      System.out.println(hoursWorked.getName()); // prints out "Untitled Metric", but should print out "hoursWorked"
  </code></pre>