Spring Boot从Bean中的数据库预加载数据 [英] Spring boot preloading data from database in bean
问题描述
我要在一个表中请求具有递归关系的元素:
I'm asking for elements that have a recursive relationship in one table:
子ID |家长ID
1 | null
2 | 1
3 | 1
4 | 2
以此类推...要询问整个数据结构,大约需要10秒钟。目前我无法重新设计该表,因为它花费了太多时间(对于更多信息:递归ORM类的Spring Repository性能问题)
And so on...To ask for this whole datastructure it takes about 10 secondsAt the moment I can't redesign this table because it cost too much time (for more information: Spring Repository performance issues with recursive ORM Class)
现在,我正在考虑在春季启动期间在Bean中预加载所有数据,以便客户端与Bean通信,并更新Bean和数据库中的数据。 。我认为启动该公司要花多长时间并不重要,但是用户必须等待它的答案才有多长。
Now, I am thinking about preloading all data during spring startup in a bean, so that the client "communicates" with the bean and I update the data in bean and database. I think it doesn't matter how long this startup takes, but it matters how long the user has to wait for its answer.
到目前为止,我没有无法对其进行预加载。我试图创建一个像这样的bean:
public class AppConfig extends WebMvcConfigurerAdapter {
...
@Autowired
SkillDAO skillDAO
...
@Bean(name="allSkills")
public List<Skill> allSkills(){
return skillDAO.findBySkill(null);
}
...
它不起作用,因为我得到了错误:
It doesn't work cause I get an error:
原因:org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名称为 dataSource的bean时出错:请求的bean当前为在创建中:是否存在无法解析的循环引用?
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?
顺便说一下,我在AppConfig中创建了所有bean。当我删除此 allSkills bean时,它再次起作用。
By the way, I create all my beans in AppConfig. When I delete this "allSkills" bean, it works again.
更新
@Component
public class MyListener {
@Autowired
private SkillDAO skillDAO;
@EventListener
public void onApplicationReady(ApplicationReadyEvent ready) {
System.out.println("++++++++++++++ HALLO +++++++++++++");
skillDAO.findBySkill(null);
}
}
...
@Bean(name="skillBean")
public MyListener myListener() {
return new MyListener();
}
推荐答案
A 缓存
可以轻松解决您的需求。
A Cache
would solve your requirement elegantly.
理想情况下,您将拥有 Service
Ideally you would have a Service
which has a method which returns the skills.
此方法看起来像这样:
import org.springframework.cache.annotation.Cacheable;
@Cacheable(value = "skills", key = "#root.methodName")
public List<Skill> getAllSkills() {
return ... your skills ...;
}
要在Spring Boot中启用缓存,请添加 @在配置类中启用EnableCaching
批注,并添加 Bean
进行配置:
To enable Caching in Spring Boot, add the @EnableCaching
annotation to your configurations class, and add a Bean
to configure it:
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("skills", "othercachename");
}
这样,方法 getAllSkills
仅在第一次调用时执行一次,然后从缓存管理器返回值,甚至不调用该方法。
This way, the method getAllSkills
is just executed once, the first time it's called, and afterwards the values are returned from the cache-manager without even calling the method.
这篇关于Spring Boot从Bean中的数据库预加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!