简体   繁体   English

有一个共同的SpringApplicationContextProvider供所有bean使用spring-managed和其他

[英]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.

相关问题 如何使用 Mockito 模拟对 spring 管理的 @Bean 的静态访问? - How to mock static access to spring-managed @Bean with Mockito? 按下JSF命令按钮两次调用了Spring管理的请求范围的Bean构造函数 - Spring-managed request scoped bean constructor called twice on press of JSF command button 如何使用Spring管理的事务性EntityManager执行自定义SQL查询 - How to execute custom SQL query with spring-managed transactional EntityManager 在托管bean中用spring注入: - injection with spring in the managed bean : Ibatis startBatch()仅适用于SqlMapClient自己的启动和提交事务,不适用于Spring管理的事务 - Ibatis startBatch() only works with SqlMapClient's own start and commit transactions, not with Spring-managed ones 如何在Spring管理的MySQL JDBC连接上设置useUnicode = true和characterEncoding = utf8属性 - How to set useUnicode=true and characterEncoding=utf8 properties on Spring-managed MySQL JDBC connection 如何重置Spring托管Bean - How to reset a Spring managed Bean 如果一个bean由Spring管理,是否意味着所有创建的bean也必须被管理 - If one bean is managed by Spring does this mean all created beans also have to be managed 托管bean中的spring服务为空 - spring service null in managed bean 注释Spring AOP AspectJ不能与JSF2视图中使用的Managed Bean中的方法一起使用吗? - Annotation Spring AOP AspectJ doesn't work with method from Managed Bean used in a JSF2 view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM