可以将Spring应用程序上下文传递给与请求关联的ThreadLocal吗?

huangapple go评论94阅读模式
英文:

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托管事件监听器的方法:

https://stackoverflow.com/questions/16019820/using-hibernate-4s-integrator-pattern-and-springs-dependency-injection/16045272#16045272

英文:

If you can use Hibernate event listeners I wrote once a response with a possible approach for Spring managed event listeners here:

https://stackoverflow.com/questions/16019820/using-hibernate-4s-integrator-pattern-and-springs-dependency-injection/16045272#16045272

huangapple
  • 本文由 发表于 2020年8月9日 10:21:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63321914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定