在服务器启动时配置Spring bean回调? [英] Configure a Spring bean callback on server startup?
问题描述
我希望我的服务器在启动时进行一些数据库清理.我尝试将@PostConstruct方法添加到服务中以执行此操作,但是@Transactional在此处不起作用.我所做的任何更改都不会保留.我怎样才能做到这一点?我可以创建一个带有特定回调的新bean吗?
I want my server to do some database cleanup on startup. I tried adding a @PostConstruct method to a service to do this, however @Transactional doesn't work there. Any changes I make aren't being persisted. How can I get this done? Can I create a new bean with specific callbacks on it?
推荐答案
您可以在@PostConstruct中调用另一个完成工作的事务资源,因为注入是在此之后完成的.因此, DbPopulator
可以使用 UserService
及其 @PostConstruct
中的任何内容,并且一切都是事务性的,并且又很好地分离了.Singleton bean是在启动afaik时构造的,因此不需要其他设置.
You can just call another transactional resource in @PostConstruct which does the work, because injection is done then. So DbPopulator
can use UserService
and whatsoever in its @PostConstruct
and everything is transactional and nicely separated again. Singleton beans are constructed at startup afaik, so no additonal setup required.
这篇关于在服务器启动时配置Spring bean回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!