[英]Having a common SpringApplicationContextProvider to be used by all bean spring-managed and otherwise
I have an ApplicationContextProvider class, which can used to access Spring ApplicationContext from beans not managed by Spring. 我有一个ApplicationContextProvider类,它可以用来从不受Spring管理的bean访问Spring ApplicationContext。 Something like mentioned here 像这里提到的东西
For the spring managed beans however I can make them ApplicationContextAware, so they can get access to ApplicationContext. 对于spring托管bean,我可以将它们设为ApplicationContextAware,这样他们就可以访问ApplicationContext。
My question is, is it a good idea to use the common ApplicationContextProvider to get spring application context from the spring-managed beans as well or should I continue to use ApplicationContextAware? 我的问题是,使用常见的ApplicationContextProvider从spring管理的bean获取spring应用程序上下文是一个好主意,还是应该继续使用ApplicationContextAware?
Using a singleton with static reference to application context is asking for trouble. 使用带有静态引用应用程序上下文的单例会遇到麻烦。 It is almost never a good idea to use such utility class in any case. 在任何情况下使用这样的实用程序类几乎都不是一个好主意。 When dealing with legacy web application use WebApplicationContextUtils
instead. 处理遗留Web应用程序时,请改用WebApplicationContextUtils
。
I once reinvented this solution in a project during migration from EJB 2.1 to Spring - but after migration we get rid of it and could sleep again. 在从EJB 2.1迁移到Spring期间,我曾经在项目中重新创建了这个解决方案 - 但是在迁移之后我们摆脱它并且可以再次睡眠。
Back to your question - using such utility for Spring managed beans has no sense at all. 回到你的问题 - 对Spring 托管 bean使用这样的实用程序根本就没有意义。 To be honest, I rarely need ApplicationContextAware
interface as well. 说实话,我也很少需要ApplicationContextAware
接口。 Spring is suppose to inject dependencies, you shouldn't ask Spring for them all the time! Spring假设注入依赖项,你不应该一直向Spring询问它们!
Can you show us some use case when you need quoted utility class (both for managed- and umanaged beans)? 当您需要引用的实用程序类(托管和非托管bean)时,能否向我们展示一些用例? Looks like you are refusing to accept support Spring gives you. 看起来你拒绝接受Spring给你的支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.