Issue
I'm loading a static list from database in the afterPropertiesSet() method.
In this class I use a static list in a lot of methods, so I don't want to load this list always from database.
The code is:
private Collection<Country> countries= null;
[...] // Use of countries in many methods
@Override
public void afterPropertiesSet() throws Exception {
// Load countries types
countries = getAddressService().loadCountries();
}
Is a good practice load collections in afterPropertiesSet()? which option would be better? I don't want to do multiples calls to DB.
Solution
I recommend using a simple cache:
@Cacheable
public Collection<Country> getCountries() throws Exception {
return getAddressService().loadCountries();
}
And then you can access them with service.getCountries()
. Only the first invocation will load from the database. All successive calls get the collection from the cache.
Answered By - Michael