0

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.

I have two small classes, a Metric, and a larger Plan:

@Component
public class Metric {

   @Value("${nameOfMetric}")
   String name;

   @Value("${empID}")
   String empID;

   @Value("${value:0.0}")
   Double value;
   }

And a larger Plan object which has three different Metrics as properties:

public class Plan {

public Metric hoursWorked;

public Metric monthlyDeals;

public Metric monthlyGoal;
...}

I also have a properties file that I am reading from:

nameOfMetric = Untitled Metric
empID = Unknown
hoursWorked = 120.0
monthlyDeals = 40.0
monthlyGoal = 100.0

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:

@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;
}

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:

    Metric hoursWorked = (Metric) ctx.getBean("hoursWorkedBean");

    System.out.println(hoursWorked.getName()); // prints out "Untitled Metric", but should print out "hoursWorked"
Yu Chen
  • 6,540
  • 6
  • 51
  • 86

2 Answers2

1

you need to autowired your metrices

public class Plan {

@Autowired   
public Metric hoursWorked;

@Autowired
public Metric monthlyDeals;

@Autowired
public Metric monthlyGoal;
...}
Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
  • Thanks Amer. Is it possible for you to go into a bit more detail about this implementation? I marked the Metrices as annotations with @Value("#{monthlyGoalBean}"), for instance, where monthlyGoalBean is a bean that instantiate in my PropertyConfig class. Am I not able to set the value of a property in one component with a bean referenced through SPEL as I am trying to do? – Yu Chen Apr 12 '17 at 15:20
1

You can use @ConfigurationProperties in a Spring Boot application.

You can have the following configuration in a Spring Boot application:

resources/application.yml

metrics:
  hoursWorked:
    name: one
    empID: 1
    value: 1.23
  monthlyDeals:
    name: two
    empID: 2
    value: 2.34
  monthlyGoal:
    name: three
    empID: 3
    value: 3.45

Metric.java (don't forget the getters/setters)

public class Metric {
    private String name;
    private String empID;
    private Double value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmpID() {
        return empID;
    }

    public void setEmpID(String empID) {
        this.empID = empID;
    }

    public Double getValue() {
        return value;
    }

    public void setValue(Double value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Metric{" +
                "name='" + name + '\'' +
                ", empID='" + empID + '\'' +
                ", value=" + value +
                '}';
    }
}

Metrics.java (Define the metric names/qualifiers/configurations)

public class Metrics {

    public static final String HOURS_WORKED = "hoursWorked";
    public static final String MONTHLY_DEALS = "monthlyDeals";
    public static final String MONTHLY_GOAL = "monthlyGoal";
}

MetricsConfiguration.java (Create beans and initialize them from configuration)

@Configuration
public class MetricsConfiguration {

    @Bean
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    @Qualifier(Metrics.HOURS_WORKED)
    @ConfigurationProperties(prefix = "metrics." + Metrics.HOURS_WORKED)
    public Metric hoursWorked() {
        return new Metric();
    }

    @Bean
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    @Qualifier(Metrics.MONTHLY_DEALS)
    @ConfigurationProperties("metrics." + Metrics.MONTHLY_DEALS)
    public Metric monthlyDeals() {
        return new Metric();
    }

    @Bean
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    @Qualifier(Metrics.MONTHLY_GOAL)
    @ConfigurationProperties("metrics." + Metrics.MONTHLY_GOAL)
    public Metric monthlyGoal() {
        return new Metric();
    }
}

DemoRunner.java (Command line runner to test the code)

@Component
public class DemoRunner implements CommandLineRunner
{
    @Autowired
    private List<Metric> allMetrics;

    @Autowired
    @Qualifier(Metrics.HOURS_WORKED)
    private Metric hoursWorkedMetric;

    @Autowired
    @Qualifier(Metrics.MONTHLY_DEALS)
    private Metric monthlyDealsMetric;

    @Autowired
    @Qualifier(Metrics.MONTHLY_GOAL)
    private Metric monthlyGoalMetric;

    @Override
    public void run(String... args) throws Exception {
        System.out.println();

        System.out.println("All metrics:");
        allMetrics.stream().forEach(System.out::println);

        System.out.println();
        System.out.println("HOURS_WORKED metric: " + hoursWorkedMetric);
        System.out.println("MONTHLY_DEALS metric: " + monthlyDealsMetric);
        System.out.println("MONTHLY_GOAL metric: " + monthlyGoalMetric);

        System.out.println();
    }
}

DemoApplication.java (SpringBoot application init)

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
andreim
  • 3,365
  • 23
  • 21
  • This is awesome. Thank you. I originally read your answer, thought it was too complicated and decided I would just mess around with it some more myself and a few days later got my beans wired up. I ended up doing exactly what you described. One follow up question- the MetricsConfiguration.java file seems quite verbose. Is there any way to reduce some of the repetitive annotations there? I'm still on the Spring learning curve. – Yu Chen Apr 16 '17 at 16:15
  • @YuChen you can place your bean configurations into as many configuration classes as you like as long as you annotate them with `@Configuration`. You can also load your items as a map into your configuration class. Thus having a map of metrics. Check [this answer](http://stackoverflow.com/questions/24917194/spring-boot-inject-map-from-application-yml) if interested to load your metrics into a map. – andreim Apr 16 '17 at 18:26
  • 1
    @YuChen if you are loading your metrics as a map then you will not need the `@Qualifier` which was used to distinguish between the beans which were of the same type - Metric. Also you will not need to keep multiple properties in your configuration class and your code will be extensible to support any number of metrics. – andreim Apr 16 '17 at 18:29