gpt4 book ai didi

java - AfterPropertiesSet 是加载 staticList 的正确位置吗?

转载 作者:行者123 更新时间:2023-12-02 02:55:53 25 4
gpt4 key购买 nike

我正在 afterPropertiesSet() 方法中从数据库加载静态列表。

在这个类中,我在很多方法中使用静态列表,所以我不想总是从数据库加载这个列表。

代码是:

 private Collection<Country> countries= null;

[...] // Use of countries in many methods

@Override
public void afterPropertiesSet() throws Exception {
// Load countries types
countries = getAddressService().loadCountries();

}

在 afterPropertiesSet() 中加载集合是一个好的做法吗?哪个选项会更好?我不想多次调用数据库。

最佳答案

我建议使用简单的缓存:

@Cacheable
public Collection<Country> getCountries() throws Exception {
return getAddressService().loadCountries();
}

然后您可以使用 service.getCountries() 访问它们。只有第一次调用才会从数据库加载。所有连续的调用都会从缓存中获取集合。

关于java - AfterPropertiesSet 是加载 staticList 的正确位置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57075690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com