我在 afterPropertiesSet() 方法中從數據庫加載靜態列表。在這個類中,我在很多方法中使用靜態列表,所以我不想總是從數據庫加載這個列表。代碼是: private Collection<Country> countries= null; [...] // Use of countries in many methods@Overridepublic void afterPropertiesSet() throws Exception { // Load countries types countries = getAddressService().loadCountries();}好的做法是在 afterPropertiesSet() 中加載集合嗎?哪個選項會更好?我不想對數據庫進行多次調用。
1 回答

慕姐4208626
TA貢獻1852條經驗 獲得超7個贊
我推薦使用一個簡單的緩存:
@Cacheable
public Collection<Country> getCountries() throws Exception {
return getAddressService().loadCountries();
}
然后您可以使用service.getCountries(). 只有第一次調用將從數據庫加載。所有連續的調用都從緩存中獲取集合。
添加回答
舉報
0/150
提交
取消