Spring不支持接口注入吗? [英] Doesn't Spring really support Interface injection at all?

查看:353
本文介绍了Spring不支持接口注入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Spring不支持接口注入,我已多次阅读过。



但是今天我看到一篇关于IOC的文章Fowler(链接),似乎在Spring中使用ApplicationContextAware是一些类似于接口注入的东西。



当我们的Spring bean需要Spring'上下文引用时,我们将实现ApplicationContextAware并实现setApplicationContext(ApplicationContext context)方法,我们将包括配置文件中的bean。这不是和Interface注入相同的吗,在那里告诉Spring注入(或),比如说,将上下文的引用传递给这个bean?



或者我是在这里遗失什么?感谢您的任何信息!



ManiKanta

解决方案

如果您的意思是接口注入在维基百科上定义,spring支持它开箱即用的ResourceLoaders,ApplicationContexts,MessageSource和其他人,使用接口 ResourceLoaderAware ApplicationContextAware MessageSourceAware ,分别./ / p>

也可以通过注册扩展这个新接口的机制一个 BeanPostProcessor



Spring参考手册解释这个功能(以及何时应该(不)使用它)非常清楚。顺便说一句,我一般认为弹簧参考手册比互联网上的人说的更可靠。


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

解决方案

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.

这篇关于Spring不支持接口注入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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