将Spring Application Context传递给与请求关联的ThreadLocal是否安全? [英] Is it safe to pass in the Spring Application Context into a ThreadLocal associated with a request?
问题描述
在JPA中,我想利用带注释的PrePersist
方法执行一些操作,而不是完成实现Singleton模式(使用getInstance()等)所需的工作,我想知道是否将Spring ApplicationContext通过一个ThreadLocal(在请求后我将其关闭)将是安全的.我只是在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.
推荐答案
如果您可以使用Hibernate事件侦听器,我会在此处编写一次响应,并提供一种针对Spring托管事件侦听器的可能方法:
If you can use Hibernate event listeners I wrote once a response with a possible approach for Spring managed event listeners here:
使用Hibernate 4的Integrator模式和Spring的依赖注入
这篇关于将Spring Application Context传递给与请求关联的ThreadLocal是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!