Spring3中注入applicationContext的最佳实践 [英] Best Practise of injecting applicationContext in Spring3

查看:34
本文介绍了Spring3中注入applicationContext的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如上面的标题,我对直接通过@Autowired 注释注入 applicationContext 或在单例 spring bean 中实现 ApplicationContextAware 接口之间的利弊感到困惑.

As in the title above, I am confused about pros cons between injecting applicationContext by directly @Autowired annnotation or implementing ApplicationContextAware interface in a singleton spring bean.

在哪些情况下您更喜欢哪一种,为什么?谢谢.

Which one do you prefer in which cases and why? Thanks.

推荐答案

实际上,两者都不好.它们都将您的应用程序与 Spring 框架联系起来,从而颠倒了整个控制反转的概念.在理想情况下,您的应用程序根本不应该知道由 ApplicationContext 管理.

Actually, both are bad. Both of them tie your application to the Spring framework, thus inverting the whole inversion-of-control concept. In an ideal world, your application should not be aware of being managed by an ApplicationContext at all.

一旦你选择违反这个原则,你怎么做就没有关系了.ApplicationContextAware 是已经存在的遗留版本 至少从 2.0 版起.@Autowired 是一种较新的机制,但它们的工作方式几乎相同.我可能会选择 ApplicationContextAware,因为它在语义上清楚地说明了它的含义.

Once you have chosen to violate this principle, it doesn't really matter how you do it. ApplicationContextAware is the legacy version that has been around at least since Version 2.0. @Autowired is a newer mechanism but they work in pretty much the same way. I'd probably go with ApplicationContextAware, because it semantically makes clear what it is about.

这篇关于Spring3中注入applicationContext的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆