0

Possible Duplicate:
How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray?

I have a class like this:

public class BankHolidayCalendar {

    List<DateTime> bankHolidays;

    public BankHolidayCalendar(final List<DateTime> p_bankHolidays) {
        bankHolidays = p_bankHolidays;
    }
}

and a property file

# holidays.properties
holidayDates=01-01-2012, 13-02-2012, 22-04-2012

How can I read these dates from this property file and inject into the bean constructor?

I am using joda time here.

Community
  • 1
  • 1
Nithin Satheesan
  • 1,546
  • 3
  • 17
  • 30
  • 1
    Duplicates: http://stackoverflow.com/questions/226050/how-do-i-specify-values-in-a-properties-file-so-they-can-be-retrieved-using-reso, http://stackoverflow.com/questions/6212898/spring-properties-file-get-element-as-an-array?lq=1 – Grzegorz Rożniecki Jun 27 '12 at 17:30

1 Answers1

3

I would use the @Value annotation and create the DateTime object in the constructor like this:

public class BankHolidayCalendar {

    List<DateTime> bankHolidays = new ArrayList<DateTime>();

    public BankHolidayCalendar(@Value("holidayDates") String[] p_bankHolidays) {
        for (String date : p_bankHolidays) {
            bankHolidays.add(...);
        }
    }
}
ryanprayogo
  • 11,587
  • 11
  • 51
  • 66
  • The sample code above can be improved by using `Arrays.asList(p_bankHolidays)` instead of the `for` loop. See [documentation of Arrays.asList](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#asList(T...)). – rmoestl Apr 10 '13 at 06:35