英文:
Is it safe to pass in the Spring Application Context into a ThreadLocal associated with a request?
问题
在JPA中,我想利用PrePersist
注解的方法来执行一些操作,但与其实现Singleton模式(使用getInstance()
等)要做的事情,我在想是否可以通过ThreadLocal传递Spring ApplicationContext来实现,然后在请求结束后关闭释放它,这样做是否安全。我主要只是使用它来getBean(Support.class)
,而不修改上下文。
英文:
In JPA I want to utilize the PrePersist
annotated method to perform some operations but rather than making the things it needs to implement the Singleton pattern (with getInstance() etc.) I was wondering if I pass the Spring ApplicationContext through a ThreadLocal (which I close release after the request) would be a safe thing to do so. I am only using it primarilly to getBean(Support.class)
and not modifying the context.
答案1
得分: 1
如果您可以使用我曾经编写的Hibernate事件监听器,我曾经在以下链接中提供了一种可能的Spring托管事件监听器的方法:
英文:
If you can use Hibernate event listeners I wrote once a response with a possible approach for Spring managed event listeners here:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论