Spring真的不支持接口注入吗?
我知道 Spring 不支持接口注入,而且我已经读过很多次了。
但今天,当我看到 Martin Fowler 写的一篇关于 IOC 的文章(链接)时,似乎在 Spring 中使用 ApplicationContextAware 与接口注入有些相似。
当我们的 Spring bean 中需要 Spring 上下文引用时,我们将实现 ApplicationContextAware 并实现 setApplicationContext(ApplicationContext context) 方法,并且我们将在配置文件中包含该 bean。这与接口注入不一样吗?接口注入告诉 Spring 注入(或者)将上下文的引用传递到这个 bean 中?
或者我在这里遗漏了一些东西?感谢您提供任何信息!
玛尼坎塔
I know that Spring doesn't supports Interface injection and I've read that many a times.
But today as I came across an article about IOC by Martin Fowler (link), it seems using ApplicationContextAware in Spring is some what similar to the Interface injection.
when ever Spring' context reference is required in our Spring bean, we'll implement ApplicationContextAware and will implement the setApplicationContext(ApplicationContext context) method, and we'll include the bean in the config file. Is not this the same as Interface injection, where where telling the Spring to inject (or), say, pass the reference of the context into this bean?
Or I m missing something here? Thanks for any information!
ManiKanta
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你指的是接口注入如wikipedia上定义的,spring支持ResourceLoaders开箱即用、ApplicationContexts、MessageSource 等,具有接口 ResourceLoaderAware,ApplicationContextAware,
还可以通过注册 BeanPostProcessor。
Spring参考手册 非常清楚地解释了这一功能(以及何时应该(不)使用它)。顺便说一句,我通常发现弹簧参考手册比“互联网上的某人”所说的更可靠。
If you mean interface injection as defined on wikipedia, spring supports it out of the box for ResourceLoaders, ApplicationContexts, MessageSource, and others, with the interfaces ResourceLoaderAware, ApplicationContextAware, MessageSourceAware, respectively.
It is also possible to extend this mechanism with new interfaces an depedencies by registering a BeanPostProcessor.
The Spring reference manual explains this capability (and when one should (not) use it) quite clearly. BTW, I have generally found the spring reference manual to be much more reliable than what 'someone on the internet' says.